Iterate values of Associative array if it has one object or more

My Problem is, I have set of arrays

$people =[{name:'Alan', hasChild:true}, {name:'Alice', hasDetail:true}];

And other one is

$people =[{name:'Alan', hasChild:true}];

I need to print all elements.

I have tried in below way

for($i=0; $i<count($people); $i++){ echo $people[$i]['name']; }

The first one is working fine. But second one show an error like this

Notice: Undefined offset: 0 in /opt/lampp/htdocs/vp/pending_users.php on line 320

Why it happen? How do i overcome this issue

-------------Problems Reply------------

This is very basic stuff. You should read a few beginner's tutorials.

You can loop the array using a foreach loop, like this:

$people = [
'name' => 'Alan',
'hasChild' => true
'name' => 'Alice',
'hasChild' => true

foreach ($people as $person) {
echo $person['name'] . "\n";

Category:php Views:71 Time:2019-03-18

Related post

  • How to check if resultset has one row or more? 2010-04-07

    How to check if resultset has one row or more with JDBC? --------------Solutions------------- ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1"); boolean isMoreThanOneRow = rs.first() &&; You didn't ask this one, but you

  • Find maximum value in associative array 2012-01-26

    I have a associative array: $users[0] = array('name' => 'Jim', 'depth' => '1', 'bk_id' => '9'); $users[1] = array('name' => 'Jill', 'depth' => '3', 'bk_id' => '10'); $users[2] = array('name' => 'Jack', 'depth' => '7', 'bk_id'

  • PHP Split Delimited String into Key/Value Pairs (Associative Array) 2011-03-13

    I have a string like so: key1\value1\key2\value2\key3\value3\key4\value4\key5\value5 And I'd like it to be an associative array so that I can do: echo $myArray['key1']; // prints value1 echo $myArray['key3']; // prints value3 etc. I know I can explod

  • Javascript. Extract values from associative array 2011-04-19

    How can I get the values from this associative array in Javascript? I just need the email addresses and not the labels. ( { office = ("[email protected]"); home = ("[email protected]"); work = ("[email protected]"); }, { home = ("[email protected]"); } )

  • Storing array as value in associative array 2010-05-11

    i have a problem where I need to have an array as a value in an associative array. Go through the code below. Here I am trying to loop the files in a directory and it is more likely that more than 1 file can have the same ctrno. So, I would like to s

  • Reading a value in associative array creates a new key 2010-06-28

    I have code such as this. I use pvalueholder is class that is polymorphic , it can hold all sort of types, string..etc.. It also can have a type undefined. typedef hash_map<pvalueholder,pvalueholder,pvaluehasher > hashtype; hashtype h; pvalueho

  • javascript set value to Associative Arrays 2011-02-03

    how set value in javascript to Associative Arrays? Why in this case i get error: "car[0] is undefined" var car = new Array(); car[0]['name'] = 'My name'; --------------Solutions------------- Because you never defined car[0]. You have to initialize it

  • Function to set default value of associative array if the key is not present 2011-02-23

    Is there a function in PHP to set default value of a variable if it is not set ? Some inbuilt function to replace something like: $myFruit = isset($_REQUEST['myfruit']) ? $_REQUEST['myfruit'] : "apple" ; --------------Solutions------------- PHP kind

  • Getting Nested Values From Associative Array 2011-03-25

    This is sort of a general implementation question. If I have an arbitrarily deep array, and I do not know before hand what the keys will be, what is the best way to access the values at specific paths of the associative array? For example, given the

  • pick random value from associated array using javascript? 2011-07-10

    Possible Duplicates: JavaScript: Getting random value from an array How can I choose an object key at random? suppose we have an array like this: var MrArray = new Array(5); MrArray['one']='oneValue'; MrArray['two']='twoValue'; MrArray['three']='thre

  • MySQL values into associative array 2011-08-16

    I have a table with the rows id, itemid, name and content, and I'm writing a function to pull different item's metadata from a table, and need the MySQL values of name and content to go into an associative array ('name'=>'content'), for mulitupl v

  • Adding multiple values to an associative array in PHP in one call 2011-10-30

    I am trying instantiate an associative array and then in a second call, assign it various other value sets on one block-line. I would like to do this following the same form as in the instantiation: "variable" = > 'value'; My instantiation is: $po

  • Iterate through two associative arrays at the same time in PHP 2011-12-06

    Given 2 associative arrays: $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); $fruits2 = array("e"=>"lemon", "f"=>"apple", "g"=>"melon", "h"=>"apple"); I would like to do something like: for ( $i = 0;

  • access each value of associative array 2011-04-05

    hello how can i access or echo each element of this associative array Array ( [0] => Array ( [rule] => admin/user [title] => Users Management [target] => _parent [0] => Array ( [0] => Array ( [rule] => admin/user/add [title] =

  • How can I search for a value in an array and delete just one of those values if 2 or more is found? 2011-06-06

    I have an array var aos = ["a","a","a","b","b","c","d","d"]; I want to know if I can remove just 1 item if it finds 2 or more of the same value in the array. So for instance if it finds "a", "a" it will remove one of those "a" This is my current code

  • Looping through entire multidimensional associative array but performing only one action in PHP 2011-10-16

    Ok, I have a multidimensional array that is populated dynamically via an API. Users can enter in arrays of information that are transformed into a multidimensional array, which would look something like this: $object = array( array( 'key1' => 'val

  • C - Change all values of an array of structures in one line 2009-08-03

    I can declare a structure: typedef struct { int var1; int var2; int var3; } test_t; Then create an array of those structs structure with default values: test_t theTest[2] = { {1,2,3}, {4,5,6} }; But after I've created the array, is there any way to c

  • associative array from javascript to activex object 2010-08-10

    I need to pass an associative array from Javascript to a C++ activex control in IE6+. It seems a shame to write my own type for something so standard. It doesn't have to work with native JS objects, I can use a custom associative array type if I need

  • getting all the values in an array except the last one 2011-08-17

    I have this right now : $s = preg_split('/\s+/', $q); $k = end($s); What I want now is to get all the values in the array $k[] except the last one, and join them in a new string. So basically if the array was : 0 => Hello 1 => World 2 => tex

  • Getting values from associative array 2009-10-08

    I have the following main array called $m Array ( [0] => Array ( [home] => Home ) [1] => Array ( [contact_us] => Contact Us ) [2] => Array ( [about_us] => About Us ) [3] => Array ( [feedback_form] => Feedback Form ) [4] =>

  • Default values for Associative array in PHP 2012-09-26

    I have a function that accepts a array parameter as array('employee_name' => 'employee_location' ) eg: array('John' => 'U.S', 'Hitler' => 'Australia', 'Unitech' => 'U.S' ) I wish to keep 'U.S' as the default location and a optional value,

  • Adding new keys & values to Associative array in PHP? 2015-01-26

    I'm building a web scraper to check prices... so I have 2 arrays and I'm trying to merge the data into one array. First array is called $results and is like so: Array ( [0] => Array ( [id] => 0 [myTitle] => Product One text... [myImage] =

  • Getting all the values of an array of Strings in one String 2009-10-10

    if I have, String[] s = new String[3]; s[0] = "Ap"; s[1] = "p"; s[2] = "le"; String result = ? If I want to get Apple out of s without looping, how do I do that? Any short cut? --------------Solutions------------- If the not looping is more important

  • Convert a javascript associative array into json object using stringify and vice versa 2011-08-17

    I have a javascript associative array like one below var my_cars= new Array() my_cars["cool"]="Mustang"; my_cars["family"]="Station Wagon"; my_cars["big"]="SUV"; I want to convert it using Stringify to json object. I want to know after conversion how

  • How to add an array value to the middle of an associative array? 2010-01-27

    Lets say I have this array: $array = array('a'=>1,'z'=>2,'d'=>4); Later in the script, I want to add the value 'c'=>3 before 'z'. How can I do this? EDIT: Yes, the order is important. When I run a foreach() through the array, I do NOT wan

Copyright (C), All Rights Reserved.

processed in 0.222 (s). 12 q(s)