5

I am trying get user value from multidimensional array as

$array = array();

$array["id"] = "1";
$array["name"] = "name1";
$array["country"] = "country1";

$array["id"] = "2";
$array["name"] = "name2";
$array["country"] = "country2";

$array["id"] = "3";
$array["name"] = "name3";
$array["country"] = "country3";

$array["id"] = "4";
$array["name"] = "name4";
$array["country"] = "country4";

foreach($array as $e){
    print_r($e);
}

It return me 4name4country4 only

I need to fetch rows like

foreach($array as $e){
    $id=$e['id'];
    $name=$e['name'];
    $country=$e['country'];
    echo $id.'/'.$name.'/'.$country.'<br>';
}

but this gives me error as Illegal string offset 'id' from what I understood about array this should return all values, Please see why this simple array is not working and suggest any way to do it

5
  • You are overwriting the keys. Commented Jun 29, 2015 at 6:14
  • @b0s3, Is there a way to mass multiple keys. Commented Jun 29, 2015 at 6:15
  • you could do it like this $temp_array["id"] = "1";$temp_array["name"] = "name1";$temp_array["country"] = "country1"; and do array_push($array, $temp_array), you should do this for each set of array Commented Jun 29, 2015 at 6:16
  • your array contains only last element.please check print_r($array); Commented Jun 29, 2015 at 6:16
  • @TallboY please see my answer ... in my answer, you will not have to specify array keys manually .... Commented Jun 29, 2015 at 6:21

3 Answers 3

3

Currently you are overwriting the keys. Need to add the keys properly. You have to build the array like -

$array[0]["id"] = "1";
$array[0]["name"] = "name1";
$array[0]["country"] = "country1";

$array[1]["id"] = "2";
$array[1]["name"] = "name2";
$array[1]["country"] = "country2";

OR

$array = array(
   0 => array('id' => 1, 'name' => 'name1', 'country' => 'country1'),
   1 => array('id' => 2, 'name' => 'name2', 'country' => 'country2'),
);
Sign up to request clarification or add additional context in comments.

Comments

3

Instead, do it like so you won't have to give array keys manually

$array = array();

$array[] = array("id" => 123,  "name" => "Your name",  "country" =>  "UK");
$array[] = array("id" => 1342,  "name" => "Your name 2 ",  "country" =>  "UK");

then in foreach do this

foreach($array as $key => $val){

echo $key. ":     ".$val['id']. " " . $val['name'];
}

2 Comments

This solution is also good but in my application I have to change values which would be easier if array has a key to it. thanks
@TallboY look at my edited answer, please see foreach loop, it has a $key`` variable which will have the key of $array values being iterated
1

You have to create the multidimensional array like this, right now you're overwriting the array multiple times.

$arrays = [
  [0]=>
    ["id"] => "1",
    ["name"] => "name1",
    ["country"] => "country1"
  ], 
  [1]=>[
     ...
  ]
];
foreach($arrays as $array){
    $id=$array['id'];
    $name=$array['name'];
    $country=$array['country'];
    echo $id.'/'.$name.'/'.'$country'.'<br>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.