PHP Get array of a property from array of objects [duplicate] - php

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
PHP JSON Specific Key To Array [duplicate]
(3 answers)
Closed 2 years ago.
I have an API that sends me the data in the format
[{"part_no":"AAA"},{"part_no":"BBB"},{"part_no":"CCC"},......{"part_no":"ZZZ"}]
I want to create an array like ["AAA", "BBB", ...., "ZZZ"] from the above array.
I know it's possible by iterating the array item by item, and appending it to a new array, but thought that there might be a better (and hopefully faster) approach.
Like C# hasLinq that does this all in a one-liner, JS has map, it is possible to do a similar thing in PHP ?

<?php
$json = '[{"part_no":"AAA"},{"part_no":"BBB"},{"part_no":"CCC"},{"part_no":"ZZZ"}]';
$data = json_decode($json, true);
$result = array_column($data, 'part_no');
var_export($result);
Output:
array (
0 => 'AAA',
1 => 'BBB',
2 => 'CCC',
3 => 'ZZZ',
)

Related

Convert PHP array to chart.js compatible data [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 3 years ago.
Am using chart.js to display charts on my site. The working data attribute for chart.js is in this format
"data" => ['1', '2', '3']
I have my data currently in a PHP array
Array ( [0] => 1 [1] => 2 [2] => 3 )
However this is not working for my needs. How can I convert the PHP array into the same format as above?
I have tried
json_encode($myArray)
and
implode($myArray)
without success.
Any suggestions?
You could create an array with key "data" and another array as the value;
$myArray = ["data" => [1, 2, 3]];
echo json_encode($myArray);
Output
{"data":[1,2,3]}
Or put the values between quotes
$myArray = ["data" => ["1", "2", "3"]];
Output
{"data":["1","2","3"]}
Not sure completely understood the problem. But if you want to convert values of php array to a json you can do something like that:
json_encode(array_values($myArray));

How to retrieve data from index of an array in php? [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 3 years ago.
It is my array:
[main] => Array (
[data] => Array
( [777] => Array (... ),
[888] => Array (....),
[999] => Array (....) ));
I usually use such syntax:
array['main']['data'][0]...
however here I don't know what exactly values are (777, 888, 999).
How could I receive this 777, 888, 999 and also theirs included data?
You can use foreach loop and you can access the values in each iteration like below :
<?php
foreach($array['main']['data'] as $item) {
print_r($item['name']);
// i assure there is some index as name in the subarray. Change the index value as per your data.
}
?>
Refer the document ion for further information PHP Foreach loop
Use foreach loop... Foreach loop. Will solve your problem

Making array into multidimensional array with same key => value [duplicate]

This question already has answers here:
Create an assoc array with equal keys and values from a regular array
(3 answers)
Closed 4 years ago.
I have an array like this
$arr = ['Hello', 'World'];
What I am trying to achieve is
$arr [
'Hello' => 'Hello',
'World' => 'World'
]
Is there a array method to achieve this or should I run a foreach loop and do it manually? I am just thinking if there is a more elegant way
You could use array_combine for this
$new = array_combine($arr, $arr);
print_r($new);

How can I convert JSON Array to array in PHP? [duplicate]

This question already has answers here:
How to convert JSON string to array
(17 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
{
"receiver_uid":[
"58a43a3e3fbbf3.61108490",
"58a43be07a3bc3.90311110",
"58da53ab5ce8d6.84754819"
]
}
This is the value I would like to convert to array. I know it's quite a simple question, but please can anyone help me?
Use json_decode
json_decode($your_jsonString,true)
You should use json_decode.
Try this:
$data = json_decode($your_json_string, TRUE);
The second parameter will make decoded json string into an associative arrays.
Example :-
$data = '{"receiver_uid":["58a43a3e3fbbf3.61108490","58a43be07a3bc3.90311110","58da53ab5ce8d6.84754819"]}';
$array = json_decode($data, true);
echo "<pre>";
print_r($array);
Output would be like
Array
(
[receiver_uid] => Array
(
[0] => 58a43a3e3fbbf3.61108490
[1] => 58a43be07a3bc3.90311110
[2] => 58da53ab5ce8d6.84754819
)
)

How to convert array into JSON object in php [duplicate]

This question already has answers here:
Converting MySQL result array to JSON [duplicate]
(2 answers)
Closed 6 years ago.
I want to convert my array value:
Array ( [page_1] => fifth [page_2] => first [page_3] => fourth [page_4] => third )
Into JSON format is given below
{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}
Can anyone please help me
You want to serialize you array.
You need to use serialize()
<?php
$a = array (
'page_1' => 'fifth',
'page_2' => 'first',
'page_3' => 'fourth',
'page_4' => 'third');
echo serialize($a);
// Outputs: a:4:{s:6:"page_1";s:5:"fifth";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"third";}
?>
$json = json_encode($array);
and otherwise
$array = json_decode($json, true);
When I insert the value in table it is inserting like
s:107:"a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}";
don't know why cause when i display it that is right but in table it is inserting something like above

Categories