I have Some API that returning me Results in JSON Format, I successfully Converted it into Multi-Dimensional Array.
Array
(
[status] => success
[cdr] => Array
(
[0] => Array
(
[date] => 2017-04-01 05:14:00
[callerid] => "ABC" <61344341227>
[destination] => 1604535320207
[description] => ABC1
[account] => ABC1
[disposition] => ANSWERED
[duration] => 10:57:57
[seconds] => 437
[rate] => 0.00200000
[total] => 0.06480000
[uniqueid] => 105943343
)
[1] => Array
(
[date] => 2017-04-11 05:10:00
[callerid] => "XYZ" <61343241227>
[destination] => 16045353250207
[description] => XYZ1
[account] => XYZ1
[disposition] => ANSWERED
[duration] => 13:57:57
[seconds] => 447
[rate] => 0.01100000
[total] => 0.06411000
[uniqueid] => 105911143
)
)
)
Kindly Help me in Fetching Data in Rows/Columns.
I wrote Below Code But it's saying undefined offset ....
foreach( $curl_jason as $key => $value)
{
echo $key;
echo $value['callerid'];
}
Try this:
foreach( $curl_jason['cdr'] as $data)
{
echo $data['callerid']; // will print callerid value
}
Related
Hi I'm trying to fill this array
Array
(
[0] => Array
(
[id] => 107
[name] => Sara
[msgSinleer] => 2
[subject] => Array
(
)
)
[1] => Array
(
[id] => 110
[name] => Manuela
[msgSinleer] => 27
[subject] => Array
(
)
)
)
with the values of this array if one of the ID match
Array
(
[0] => Array
(
[id] => 110
[Cliente] => Alexis
[body] => Hola
[fecha] => 06/01/2021 11:35 AM
[status] => 1
[tiempoSinLeer] => 15 01:00
)
[1] => Array
(
[id] => 110
[Cliente] => Alexis
[body] => Hola asd asdasd
[fecha] => 05/26/2021 11:35 AM
[status] => 1
[tiempoSinLeer] => 21 01:00
)
[2] => Array
(
[id] => 107
[Cliente] => George
[body] => vhgfshsgf
[fecha] => 06/15/2021 03:48 PM
[status] => 1
[tiempoSinLeer] => 1 08:48
)
)
So I'm trying to show the result in this way but I don't know how to do it, I tried some others methods like array_search, array_in and a foreach inside another foreach trying to see if inside can do an if statement but it doesn't work.
Array
(
[0] => Array
(
[id] => 107
[name] => Sara
[msgSinleer] => 2
[subject] => Array
(
[Cliente] => George
[body] => vhgfshsgf
[fecha] => 06/15/2021 03:48 PM
[status] => 1
[tiempoSinLeer] => 1 08:48
)
)
[1] => Array
(
[id] => 110
[name] => Manuela
[msgSinleer] => 27
[subject] => Array
(
[Cliente] => Alexis
[body] => Hola
[fecha] => 06/01/2021 11:35 AM
[status] => 1
[tiempoSinLeer] => 15 01:00
),
(
[Cliente] => Alexis
[body] => Hola asd asdasd
[fecha] => 05/26/2021 11:35 AM
[status] => 1
[tiempoSinLeer] => 21 01:00
)
)
)
And this is the code that I have that bring me the arrays
foreach ($query2 as $key2) {
$result2[] = $key2;
}
foreach ($query as $value) {
array_push($result , array(
"id" => $value['id'],
"name" => $value['name'],
"msgSinLeer" => $value['msgSinleer'],
"subject" => array(),
));
}
print_r ($result);
print_r ($result2);
I'm new in php so if someone can help me I will appreciate
Following logic might help you on your way. Cycle through array $arr0 to find matching id's in $arr1. When a match is found, push to the 'subject' element in $arr0.
foreach($arr0 as $key => $record) {
foreach($arr1 as $subject) {
if($record['id'] === $subject['id']) $arr0[$key]['subject'][] = $subject;
}
}
demo
So I'm using curl to return some JSON from an API and I'm using json_decode($result,true);
I have managed to access the title but I need to loop through the whole array and output the title, url, location and picture.
I'm doing a foreach to print all the content:
<?php
foreach ($json as $value) {
echo "<pre>";
print_r($json);
echo $json['data'][2]['title'];
}
?>
What blocks me its how do I access every element.
Array
(
[paging] => Array
(
[total_items] => 33
[current_page] => 1
[total_pages] => 3
)
[data] => Array
(
[0] => Array
(
[id] => 776583
[title] => NAME
[url] =>URL
[status] => open
[current_status] => open
[location] => a, a
[programmes] => Array
(
[id] => 2
[short_name] => SHORT NAME
)
[applications_count] => 5
[is_favourited] =>
[branch] => Array
(
[id] => 319532
[name] => International SOS - 1
[organisation_id] => 318911
[profile_photo_url] => URL
[url] => URL
)
[views] => 248
[duration_min] => 22
[duration_max] => 24
[applications_close_date] => 2016-09-06T00:00:00.000Z
[earliest_start_date] => 2016-10-01T00:00:00.000Z
[latest_end_date] => 2017-04-01T00:00:00.000Z
[profile_photo_urls] => Array
(
[original] => PIC URL
[medium] => PIC URL
[thumb] => PIC URL
)
[cover_photo_urls] => PNG IMG
[created_at] => 2016-08-30T03:24:41Z
[updated_at] => 2016-08-30T15:36:02Z
)
[1] => Array
(
[id] => 774984
[title] => NAME
[url] => URL
[status] => open
[current_status] => open
[location] => Bonn, Germany
[programmes] => Array
(
[id] => 2
[short_name] => NAME
)
[applications_count] => 128
[is_favourited] =>
[branch] => Array
(
[id] => 287321
[name] => Deutsche Post DHL Group
[organisation_id] => 286836
[profile_photo_url] => PHOTO
[url] => URL
)
[views] => 1331
[duration_min] => 48
[duration_max] => 48
[applications_close_date] => 2016-09-04T00:00:00.000Z
[earliest_start_date] => 2016-10-01T00:00:00.000Z
[latest_end_date] => 2017-10-01T00:00:00.000Z
[profile_photo_urls] => Array
(
[original] => PIC
[medium] => PIC
[thumb] => PIC
)
[cover_photo_urls] => PIC
[created_at] => 2016-08-23T19:47:04Z
[updated_at] => 2016-08-24T06:35:58Z
)
<?php
foreach ($json as $value) {
foreach($value['data'] as $inner){ //looping json['data']
echo $inner['id'];
echo $inner['title'];
foreach($inner['programmes'] as $in_inner){ //looping json['data']['element_number']['programmers']
echo $in_inner['id'].':'.$in_inner['short_name'];
}
}
}
?>
In this way you can iterate through inner arrays.
My data is in a second tier of a return value that I am trying to access the individual elements. Array ( [success] => 1 [return] => Array ( [0] => Array
I tried ['return'] since it is the key of that, is what I need but got the same error or obvious worse.
error:
Array to string conversion in orders.php on line 10
code:
<?php
$id = $argv[1]; //variable for inbound
require_once('phpPlay.php');
$result = api_query("mytrades", array("marketid" => $id));
foreach( $result as $x) {
echo $x;
}
?>
data top 5 rows:
Array
(
[success] => 1
[return] => Array
(
[0] => Array
(
[tradeid] => 74038377
[tradetype] => Sell
[datetime] => 2014-11-12 16:05:32
[tradeprice] => 0.00675000
[quantity] => 22.18670000
[fee] => -0.00007488
[total] => 0.14976023
[initiate_ordertype] => Buy
[order_id] => 197009493
)
[1] => Array
(
[tradeid] => 73687280
[tradetype] => Buy
[datetime] => 2014-11-09 03:38:13
[tradeprice] => 0.00816988
[quantity] => 0.00100000
[fee] => 0.00000002
[total] => 0.00000817
[initiate_ordertype] => Buy
[order_id] => 194824864
)
[2] => Array
(
[tradeid] => 73684313
[tradetype] => Sell
[datetime] => 2014-11-09 02:57:41
[tradeprice] => 0.00808034
[quantity] => 0.00100000
[fee] => 0.00000000
[total] => 0.00000808
[initiate_ordertype] => Buy
[order_id] => 194803992
)
[3] => Array
(
[tradeid] => 73653019
[tradetype] => Sell
[datetime] => 2014-11-08 17:53:12
[tradeprice] => 0.00793991
[quantity] => 0.00010000
[fee] => 0.00000000
[total] => 0.00000079
[initiate_ordertype] => Buy
[order_id] => 194559503
)
[4] => Array
(
[tradeid] => 73652717
[tradetype] => Sell
[datetime] => 2014-11-08 17:50:13
[tradeprice] => 0.00793989
[quantity] => 0.00100000
[fee] => 0.00000002
[total] => 0.00000794
[initiate_ordertype] => Sell
[order_id] => 194559596
)
...
The problem is that with the echo $x statement in your code above, $x is an array, not a string.
The echo function requires a string, so the error you're getting is because PHP is automatically trying to convert the parameter passed to echo to a string, but it is failing because you are passing an array.
Try this:
if (isset($result['return'])) {
foreach($result['return'] as $result_item) {
echo(var_export($result_item, true));
}
} else {
echo 'No results';
}
I try to parse my elasticsearch response in php.
When i print my json resultat with a var_dump, i've got that:
Array (
[took] => 6
[timed_out] =>
[_shards] => Array (
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array (
[total] => 1
[max_score] => 0.44896343
[hits] => Array (
[0] => Array (
[_index] => car
[_type] => car
[_id] => DqE0c4ygRgC81o39DNmwhQ
[_score] => 0.44896343
[_source] => Array (
[currency] => EUR
[link] => myrurl
[reference] => A785454A
[brand] => mybrand
[model] => mymodel
[description] =>
[link_picture] => mylinkpicture
[price] => myprice
[km] => mykm
[start_years] =>
[active] => 1
[title] => mytitle
[ranking] => 22
[date_create] => 2014-05-26
)
)
)
)
)
But, when i try to list or to find a title by a foreach:
$myData = json_decode($response);
foreach ($myobj->hits->hits as $result) {
echo $result->_source->title;
}
that doesn't work, because i obtain this error:
Message: Invalid argument supplied for foreach()
I don't see why.
Thanks in advnce.
In foreach you are trying to use "$myobj".
But decoded json is in "$myData".
Array
(
[abc] => Array
(
[0] => Array
(
[id] => 1
[title] => hello 12
[meta_keyword] =>
[meta_description] =>
[tags] => sdfgdfg
[status] => draft
[body] => dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 5
[subcatid] => 7
[featured] =>
[pubdate] => 2011-06-17 03:39:55
[user_id] => 0
)
[1] => Array
(
[id] => 2
[title] => hello xyz
[meta_keyword] =>
[meta_description] =>
[tags] => xcfasdfcasd
[status] => draft
[body] => dfdsafsdf dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 1
[subcatid] => 2
[featured] =>
[pubdate] => 2011-06-17 03:43:12
[user_id] => 0
)
for example if i want to echo out title I would do echo $abc['title'] but it's not working pls help,
the above output is a result of print_r($count['abc]);
it shows nothing when i do print_r($count['abc']['title'])
You would need to use the numeric key as well: $abc[0]['title'].
In other words, you've got an array with array members of an array type which use numeric keys, in which each of those members are arrays which use associative keys to access values. So you need to access each array in $abc to get to the array which contains your title values.
EDIT
If you're trying to loop through these values, you would need to loop through each array. Such as:
$c_abc = count($abc);
for ($i = 0; $i < $c_abc; $i++) {
echo "{$abc[$i]['title']}<br/>";
}
Read about php associative arrays....you will have you goal achieved
try this:
foreach ($array as $i => $values) {
print "$i {\n";
foreach ($values as $key => $value) {
print " $key => $value\n";
}
print "}\n";
}
To access you array variables, the right way is like this
$count['abc'][0]['title']
However, in your title, you are asking about Array keys as variables?
Actually this does not need to be related with CI.
A simple example
$array = array ( "hi" => "bye");
extract( $array);
//it will make "hi" a variable :: $hi = "bye"
echo $hi; // will output bye
Heres structured solution
$data = Array(
[abc] => Array
(
[0] => Array
(
[id] => 1
[title] => hello 12
[meta_keyword] =>
[meta_description] =>
[tags] => sdfgdfg
[status] => draft
[body] => dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 5
[subcatid] => 7
[featured] =>
[pubdate] => 2011-06-17 03:39:55
[user_id] => 0
)
[1] => Array
(
[id] => 2
[title] => hello xyz
[meta_keyword] =>
[meta_description] =>
[tags] => xcfasdfcasd
[status] => draft
[body] => dfdsafsdf dsfdsf dfdsafsdfsdfsdf
[photo] => images/blog/nari.jpg
[raw] => nari
[ext] => .jpg
[views] => 0
[video] =>
[categoryid] => 1
[subcatid] => 2
[featured] =>
[pubdate] => 2011-06-17 03:43:12
[user_id] => 0
)
)
);
extract($data);
foreach($abc as $value){
echo $value['title']."<br>";
}