Home > Enterprise >  Issue with the loop foreach php
Issue with the loop foreach php

Time:06-06

sorry if my question seems stupid, I'm new to php. I try to create a loop on my array but the loop returns me only the last value. I don't understand and I tried everything

$categories = array('name' => 'mamals', 'id' => '1');
$categories = array('name' => 'birds','id' => '2');
$categories = array('name' => 'fishs', 'id' => '3');
$categories = array('name' => 'reptiles', 'id' => '4');

$category = $categories;

foreach($category as $key =>$categ){
    echo $categ;
}

It return only "reptiles 4" ! Thank you for you answers

CodePudding user response:

You are overwriting the variable categories, i modified the code by initializing the categories with an empty array, then pushing your entries in it.

$categories = [];
array_push($categories, array('name' => 'mamals', 'id' => '1'));
array_push($categories, array('name' => 'birds','id' => '2'));
array_push($categories, array('name' => 'fishs', 'id' => '3'));
array_push($categories, array('name' => 'reptiles', 'id' => '4'));

foreach($categories as $key=>$categ){
    echo "ID: " . $categ["id"] . ", NAME: " . $categ["name"];
}

CodePudding user response:

I response to shunz19's anwser where I said:

It would help to show the shorthand for this mechanism as well. I don't think anyone would use array_push in this sitation.

Here is a more concise solution:

Cause:

You are overwriting your variable - $categories - each time you use =. So after line 3 the only value in $categories is :

categories = array('name' => 'reptiles', 'id' => '4');

Step By Step:

You look like you want to be adding entries to the Categories multidimensional array. Therefore you need to tell PHP to add not to overwrite, typically with the [] indicating the value is to be inserted into a new (incremental) variable key.

$categories = array('name' => 'mamals', 'id' => '1');
$categories[] = array('name' => 'birds','id' => '2');

This will increment the key index (numeric) by 1 and set the value of array into that key.

It is standard practise to establish numeric arrays and then populate them with this style of referencing.

But this isn't quite simple...

Because your parent array contains sub-arrays, your foreach will give warnings and errors because:

Warning: Array to string conversion in /home/user/scripts/code.php on line XX

Can you see why? Yes, because your foreach is only opening the parent array, not the child arrays so the data types within are still arrays, but you want to output them as if they're strings.

How can you do this? With a fun little function called print_r().

Concise Solution and Fix:

$categories = []; // Establish the var type is an array.
$categories[] = array('name' => 'mamals', 'id' => '1'); // Add to the array.
$categories[] = array('name' => 'birds','id' => '2'); // add more,... 
$categories[] = array('name' => 'fishs', 'id' => '3');
$categories[] = array('name' => 'reptiles', 'id' => '4');

$category = $categories;

foreach($category as $key =>$categ){
     print_r($categ);
}

Output:

Array
(
    [name] => mamals
    [id] => 1
)
Array
(
    [name] => birds
    [id] => 2
)
Array
(
    [name] => fishs
    [id] => 3
)
Array
(
    [name] => reptiles
    [id] => 4
)

Code Example:

You can alternatively just access the array names from the froeach such as:

foreach($category as $key =>$categ){
     print $categ['name']."\n"; // will list each name starting at the lowest array key. 
}

See my test code here.

  •  Tags:  
  • php
  • Related