How do I access certain elements of a PHP multidimensional array? - php

I have the following multidimensional array.
I have run a foreach loop and am trying to echo the client nicknames of the 2 users. The code I am running is as follows.
$client_r = $Ts3->clientList();
foreach ($client_r as $client)
{
echo $client['data']['client_nickname'];
}
What am I doing wrong?

Try this:
foreach ($client_r as $client)
{
echo $client['data'][0]['client_nickname'];
echo $client['data'][1]['client_nickname'];
// As there are further arrays inside array, so you have to include the index also
}

If you start from the data then your access will be easy-
In data you have again array, so the $client now stores the sub array, now you can easily access your client_nickname.
$client_r = $Ts3->clientList();
foreach ($client_r['data'] as $client){
echo $client['client_nickname'];
}
This may be solve your problem.

Related

How to visit an array indexed by indexed in php / Laravel?

I got an API response like
["at123#gmail.com","adhd5#gmail.com","adahsad5#gmail.com"]
I got this array in a request variable $request->optional_email I am trying to access data by a loop like below:
foreach ($request->optional_email as $key => $optionalEmail) {
$email->email = $optionalEmail[$key];
$email->save();
}
But it doesn't work. How can I solve it?
As the $request->optional_email is just a list you do not need to use the $key variable in the foreach.
Instead you should just use the value ($optionalEmail) of the foreach so your code would look something like this:
foreach ($request->optional_email as $optionalEmail) {
$email->email = $optionalEmail;
$email->save();
}

Accessing JSON array data in PHP

I am trying to get some data from an external API. From the JSON data I want to retrieve some data as marked in the image.
I can access the "teams" data by using this code -
foreach( $data->data as $info ) {
echo '<li class="team-1">'. $info->teams[0].'</li>';
echo '<li class="team-2">'. $info->teams[1].'</li>';
};
But when I try to access data more deep from array of objects it doesn't work & gives me error -
foreach( $info->sites as $site) {
foreach( $site->odds as $odd) {
echo $odd->h2h[0];
}
}
So my question is what is the best way to loop through the data to access these arrays. I am using this code in Wordpress but I think it will be same as PHP.
You should access h2h directly from odds, since it's not an array, but an object
foreach( $info->sites as $site) {
echo $site->odds->h2h[0];
}
It looks like odds is an object rather than an array.
If h2h will always be the only property in odds then you can try:
foreach( $info->sites as $site) {
echo $site->odds->h2h[0];
}
Alternatively, if h2h will not always be the only property in odds, then you may want to try casting it into an array:
foreach( $info->sites as $site) {
foreach( (array)$site->odds as $odd) {
echo $odd[0];
}
}
You get this json from an external api and convert it using json_decode, right? If that is so, just use the second parameter "$assoc" and set it to true to not get an object, but an associative array that you can use like this:
$info = json_decode($api_answer,true);
if(isset($info['sites']['odds'])){
foreach($info['sites']['odds'] as $odd){
echo $odd['h2h'][0]
}
}

Loop through nested arrays PHP

Im trying to loop through PHP arrays decoded from a json file. I get the results but it only gives me the first results of the arrays in the the file. How can I make it loop? This is my code:
foreach ($events as $event) {
echo $event['d']['Tree1'][0]['Tree2']['Field1'] . '<br>';
echo $event['d']['Tree1'][0]['Tree2']['Field2'] . '<br>';
echo $event['d']['Tree1'][0]['Tree2']['Field3'] . '<br>';
}
Sounds like you're trying to loop through the values of a "multidimensional array". You're starting correctly by going through your array with a loop, but then you're stuck because each element in your loop is... another array. So, to echo out the values of the child array, you want to run a second loop inside of your loop. Essentially, if your loop hits a child array, you want to loop through that array too. If you know your array is made of child arrays only, you can do this like so:
<?php
foreach ($events as $event) {
foreach($event as $ev) {
echo $ev;
}
}
If you need the keys, that adds a slight layer of complexity, but nothing you can't manage.
<?php
foreach ($events as $event) {
foreach ($event as $k=>$v) {
echo $k .': '. $v;
}
}
There are some examples in the php manual as well. You can also add in conditionals if you only need data from specific keys. Good luck!

I want a query result using codeigniter

i have an array result. i want to print 2 rows(product data) in first page.
next 2 rows in second page and so on. if anybody knows this,please help me to solve it
my array
$data['product_list']
foreach($data['product_list'] as $dat)
{
echo $dat->prd_id;
echo $dat->prd_name;
}
You are doing a foreach loop on an associative array and then trying to access the the contents as objects by using ->. I can only given assumption of what you might be doing. If your array is already populated with a name and id like you have described in your foreach loop this is how you would access the contents in the loop:
foreach($data['product_list'] as $dat)
{
echo $dat['prd_id'];
echo $dat['prd_name'];
}
That is how you would print out the contents providing you had the data stored in your array like so:
$data['product_list'][0] = array('prd_id'=>'id0','prd_name'=>'name0');
$data['product_list'][1] = array('prd_id'=>'id1','prd_name'=>'name1');
$data['product_list'][2] = array('prd_id'=>'id2','prd_name'=>'name2');
Better you try with array_slice();
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>

PHP array unique on multi dimensional array/object

I've been trying for a while. I have tried several things to fix this, but I just can't get it to work.
My code:
<?php
if (is_array($row))
{
foreach ($row as $data) {
echo array_unique($data->username);
}
}
?>
It gives me the following error
Message: array_unique() expects parameter 1 to be array, string given
I have no idea what is going on with this. I have even tried placing the array_unique in the $row.
So like:
<?php
if (is_array($row))
{
foreach (array_unique($row) as $data) {
echo $data->username;
}
}
?>
But this gives me another error:
Object of class stdClass could not be converted to string
I have no idea what's going on. I have searched for hours but haven't found anything on here. Any help is greatly appreciated. Thanks.
You can't use array_unique on multi-dimensional arrays when you're looking inside the depth. Its works on flat one, and certainly won't work on strings. An alternative is to create another container for that and use usernames as keys, then you'll get unique ones.
Since you haven't shown the array/object structure, here a little bit on an idea on the comment I gave above:
$container = array();
foreach($row as $data) {
if(!isset($container[$data->username])) {
$container[$data->username] = $data;
}
}
// $container = array_values($container); // optional simple reindex

Categories