PHP - How to generate unconventional data from a multidimensional array - php

after 10 years I’m starting programming again, just for fun. I’m struggling with the following code in PHP:
This is what I get from the database:
$data[0] = ['id' => NULL, 'titel' => NULL, 'category_id' => NULL];
$data[1] = ['id' => '1', 'titel' => 'Apple', 'category_id' => '123'];
$data[2] = ['id' => '2', 'titel' => 'Pear', 'category_id' => '123'];
$data[3] = ['id' => '3', 'titel' => 'Orange', 'category_id' => '123'];
$data[6] = ['id' => '6', 'titel' => 'Strawberry', 'category_id' => '297'];
$data[7] = ['id' => '7', 'titel' => 'Blueberry', 'category_id' => '297'];
$data[8] = ['id' => '8', 'titel' => 'Raspberry', 'category_id' => '297'];
$data[12] = ['id' => '12', 'titel' => 'Cucumber', 'category_id' => '557'];
$data[13] = ['id' => '13', 'titel' => 'Pumpkin', 'category_id' => '557'];
That’s the string I have to generate for the javascript:
'choices': {
0: {
text: [''],
value: ['']
},
123: {
text: ['Apple', 'Pear', 'Orange'],
value: ['1', '2', '3']
},
297: {
text: ['Strawberry', 'Blueberry', 'Raspberry'],
value: ['6', '7', '8']
},
557: {
text: ['Cucumber', 'Pumpkin'],
value: ['12', '13']
}
}
Can someone help me on that one? I’m trying something like this, but struggling with:
$text = 'text: [';
$value = 'value: [';
$final_str = '';
$current_category_id = '';
foreach ($data as $foo) {
if (($foo['category_id'] != $current_category_id)) {
$text .= ']';
$value .= ']';
$final_str .= '}, ' . $product_id . ': { ' . $text . $value;
$text = 'text: [';
$value = 'value: [';
}
$text .= '\'' . $foo['titel'] . '\', ';
$value .= '\'' . $foo['id'] . '\', ';
$current_category_id = $foo['category_id'];
}
$text .= ']';
$value .= ']';
$final_str .= $current_category_id . ' { ' . $text . $value;
Thank you very much!

First things first: don't try to build that string by hand, you'll just get headaches. Instead, build up the array structure in PHP, and then use json_encode to convert it to JSON.
You can then either cheat and use the JSON directly as a JavaScript variable (always remember that JSON and JavaScript are not the same language, but JSON is almost always valid JavaScript), or use JSON.parse to parse it properly.
The next trick is that since you're building an associative array, you don't need to track the "current" category, you can just add each item into the right place:
$data_by_category = [];
foreach ( $data as $item ) {
// Force the vategory to be an integer, 0 for null
$category_id = (int)$item['category_id'];
// Set up the category the first time you see it
$data_by_category[$category_id] ??= ['text'=>[], 'value'=>[]];
// Add the items as you see them, forcing them to be strings
$data_by_category[$category_id]['text'][] = (string)$item['titel'];
$data_by_category[$category_id]['value'][] = (string)$item['id'];
}

Instead of manually building a string, you should generate the data structure and then output it via json_encode:
<?php
$return = [];
foreach($data as $d) {
$cat_id = intval($d['category_id']);
if (!isset($return[$cat_id])) {
$return[$cat_id] = [
'text' => [],
'value' => [],
];
}
$return[$cat_id]['text'][] = strval($d['titel']);
$return[$cat_id]['value'][] = strval($d['id']);
}
?>
<script>
...
'choices': <?= json_encode($return); ?>
...
</script>
If you do echo json_encode($return); it will output:
{"0":{"text":[""],"value":[""]},"123":{"text":["Apple","Pear","Orange"],"value":["1","2","3"]},"297":{"text":["Strawberry","Blueberry","Raspberry"],"value":["6","7","8"]},"557":{"text":["Cucumber","Pumpkin"],"value":["12","13"]}}
You can see it in action at https://3v4l.org/VrHXO

Related

php get object data from array of field names

I have a result set from a query that gives me an object that has dozens of fields, the below example is a subset:
[79] => stdClass Object
(
[name] => John Doe
[email] => john#doe.com
[ext] => 4004
[options] => stdClass Object
(
[type] => friend
[rating] => Excellent
[context] => default
)
[address] => 123 Anywhere St
)
Instead of plowing through each field, because I only want a handful of them, I am trying to use an array to get what i want:
$fields = array('name','email','options->type','options->rating','address');
so then i do:
foreach ($result as $k => $v){
foreach ($fields as $kk => $vv){
echo $kk. " - " . $v->$kk."<br>";
}
}
Which gives me field name and its value.
name - John Doe
email - john#doe.com
address - 123 Anywhere St.
However, anything in that sub object(options) is giving me a blank result.
You can use a recursive function providing that you don't mind changing the format of your $fields var to include arrays. In my opinion this makes it easier to read anyway, and easier to handle in code.
The benefit of using a recursive function is that it will handle any depth.
$o = (object) [
'name' => 'John Doe',
'email' => 'john#doe.com',
'ext' => 4004,
'options' => (object) [
'type' => 'friend',
'rating' => 'Excellent',
'context' => 'default',
'any' => (object) [
'depth' => (object) [
'will' => 'work'
]
]
],
'address' => '123 Anywhere St'
];
$fields = [
'name',
'email',
'options' => [
'type',
'rating',
'any' => [
'depth' => [
'will'
]
]
],
'address'
];
function getObjectProps($o, $fields, $parent = '') {
if (strlen($parent)) {
$parent .= '->';
}
foreach ($fields as $k => $v) {
if (is_array($v)) {
getObjectProps($o->{$k}, $v, $parent . $k);
} else {
echo $parent . $v . ' - ' . $o->{$v} . '<br/>';
}
}
}
getObjectProps($o, $fields);
Output:
name - John Doe
email - john#doe.com
options->type - friend
options->rating - Excellent
options->any->depth->will - work
address - 123 Anywhere St
Use a multidimensionnal array for $fields and change your foreach loop a little bit:
$fields = [
'name',
'email',
'options' => [
'type',
'rating'
],
'address'
];
foreach ($result as $obj) {
foreach ($fields as $k => $v) {
if (is_array($v)) {
foreach ($v as $vv) {
echo $vv . ' - ' . $obj->$k->$vv . '<br>';
}
} else {
echo $v . ' - ' . $obj->$v . '<br>';
}
}
}
Here's what I came up with in the meantime
$someObject = new stdClass();
$someObject->name = 'Dale';
$someObject->options = new stdClass();
$someObject->options->address = 'The Address';
$fields = ['name', 'options' => ['address']];
function toArray($object, $fields) {
$return = [];
foreach($fields as $fieldKey => $fieldValue) {
if(!is_array($fieldValue)) {
$return [] = $object->$fieldValue;
}
else
{
$return = array_merge($return, toArray($object->$fieldKey, $fieldValue));
}
}
return $return;
}
print_r(toArray($someObject, $fields));
Try this code:
$newResult = array_intersect_key((array)$result, array_flip($fields));
There is no easy way to access property like the way you are trying. But there's a symfony's property access component which you can use. But if you are focusing to use your own method then you might love the following code. I ran this code in PHP 7.2
$data = (object) [
'name' => 'John Doe',
'email' => 'john#doe.com',
'ext' => 4004,
'options' => (object) [
'type' => "friend",
'rating' => 'Excellent',
'context' => 'default'
],
'address' => '123 Anywhere St'
];
$fields = array('name','email','options->type','options->rating','address');
/*** way of initializing
$response = []; // get array value here
$response = ""; // get string value here
***/
$response = ""; //string value here... as output
/*** Don't you think that the below code now is clean and reusable? ***/
array_map(function($key) use($data, &$response) {
$value = array_reduce(explode('->', $key), function ($o, $p) {
return $o->$p;
}, $data);
is_array($response) ? ($response[$key] = $value) : ($response .= $key . ' - ' . $value . '<br>');
}, $fields);
echo $response;
In the above code if you initialize $response as an empty array then you'll get array as output. And if you initialize that as an empty string then you'll get string output. And I think instead of using for loop, use array_map and array_reduce, they work like magic and reduce your code too.
String as output:
Array as ouput:

Access and join two columns in an array separately

I have below array,
Array
(
[1] => Array
(
[0] => Array
(
[location] => X33
[usernumber] => 1
[order] => XX
[part_number] => Hi
)
[1] => Array
(
[location] => X33
[usernumber] => 1
[order] => YY
[part_number] => 68730
)
)
I want desired output string to echo as below,
'Hello ur oder - XX, YY, part number-Hi, 68730'
How to achieve this output? I was thinking to run a foreach loop, but I'm not sure how I could convert this to a string.
Run a foreachloop and concat
$orderNumber = '';
$partnumber = '';
foreach ($yourArray as $array) {
if ($orderNumber !="") {
$orderNumber.=",";
}
$orderNumber.= $array['order'];
if ($partNumber !="") {
$partNumber.=",";
}
$partNumber.= $array['part_number'];
}
echo "Hello ur order - ".$orderNumber." part number-". $partNumber;
#Frayne Konoks Solution is a nice oneliner:
His Solution with quotes fixed :)
echo 'Hello, ur order - '.implode(", ", array_column($var[1], 'order')).', '.'part number-'.implode(", ", array_column($var[1], 'part_number'))."'";
$var is your array.
Working example:
http://sandbox.onlinephpfunctions.com/code/c95545bdf9d9216d6c80cc06542517e596a0360a
Your must help this code:
<?php
$array = [
[
[
'order' => 1,
'part_number' => 1
],
[
'order' => 2,
'part_number' => 2
]
]
];
$orders = [];
$partnumber = [];
foreach($array as $v) {
foreach($v as $item) {
$orders[] = $item['order'];
$partnumber[] = $item['part_number'];
}
}
$str = sprintf("Hello ur oder - %s, part number-%s", implode(', ', $orders), implode(', ', $partnumber));
echo $str;
Result:
Hello ur oder - 1, 2, part number-1, 2
I changed the array structure a little bit. The code below would be of help:
$arr = array(
array(
'location' => 'X33',
'usernumber' => '1',
'order' => 'XX',
'part_number' => 'Hi',
),
array(
'location' => 'X33',
'usernumber' => '1',
'order' => 'YY',
'part_number' => '68730',
)
);
$order = '';
$p_no = '';
foreach ($arr as $ar) {
$order .= $ar['order'] . ',';
$p_no .= $ar['part_number'] . ',';
}
$order = rtrim($order, ',');
$p_no = rtrim($p_no, ',');
$final_str = 'Hello ur order - ' . $order . ' part number - ' . $p_no;
echo $final_str;
It's as simple as:
foreach($array[1] as $item){
echo "Hello ur order - " . $item["order"] . ", " $item["location"] . ", part number-" . $item["order"] . ", " . $item["part_number"];
}

Convert associative multidimensional array into single-dimensional array with treepath keys [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP convert nested array to single array while concatenating keys?
Get array's key recursively and create underscore seperated string
Please, read the whole question before answering.
I have this multidimensional array:
$data = array(
'user' => array(
'email' => 'user#example.com',
'name' => 'Super User',
'address' => array(
'billing' => 'Street 1',
'delivery' => 'Street 2'
)
),
'post' => 'Hello, World!'
);
I want it flatten, transformed into:
$data = array(
'user.email' => 'user#example.com',
'user.name' => 'Super User',
'user.address.billing' => 'Street 1',
'user.address.delivery' => 'Street 2',
'post' => 'Hello, World!'
);
Important:
The keys are very important to me. I want them concatenated, separated by periods.
It should work with any level of nesting.
Thank you!
Something like this should work:
function flatten($array, $prefix = '') {
$result = array();
foreach($array as $key=>$value) {
if(is_array($value)) {
$result = $result + flatten($value, $prefix . $key . '.');
}
else {
$result[$prefix . $key] = $value;
}
}
return $result;
}
DEMO
Thanks for all the given answers.
I have transformed it in the following, which is an improved version. It eliminates the need of a root prefix, does not need to use references, it is cleaner to read, and it has a better name:
function array_flat($array, $prefix = '')
{
$result = array();
foreach ($array as $key => $value)
{
$new_key = $prefix . (empty($prefix) ? '' : '.') . $key;
if (is_array($value))
{
$result = array_merge($result, array_flat($value, $new_key));
}
else
{
$result[$new_key] = $value;
}
}
return $result;
}
Try this
<?php
$data = array(
'user' => array(
'email' => 'user#example.com',
'name' => 'Super User',
'address' => array(
'billing' => 'Street 1',
'delivery' => 'Street 2'
)
),
'post' => 'Hello, World!'
);
function prefixKey($prefix, $array)
{
$result = array();
foreach ($array as $key => $value)
{
if (is_array($value))
$result = array_merge($result, prefixKey($prefix . $key . '.', $value));
else
$result[$prefix . $key] = $value;
}
return $result;
}
var_dump(prefixKey('', $data));
?>
Outputs
array
'user.email' => string 'user#example.com' (length=16)
'user.name' => string 'Super User' (length=10)
'user.address.billing' => string 'Street 1' (length=8)
'user.address.delivery' => string 'Street 2' (length=8)
'post' => string 'Hello, World!' (length=13)
test this out here
i passed by reference so no need for returns. just hand over the array storage.
$store = array();
function flatten($array,&$storage,$parentKey = ''){
foreach($array as $key => $value){
$itemKey = (($parentKey)? $parentKey.'.':'').$key;
if(is_array($value)){
flatten($value,$storage,$itemKey);
} else {
$storage[$itemKey] = $value;
}
}
}
flatten($data,$store);
var_dump($store);
Use recursion such as this:
function process_data( $data, $parent_key ){
if ( ! is_array( $data ) ){
return $data;
}
$flattened_array = array();
foreach( $data as $key => $item ){
$flattened_key = $parent_key . '.' . $key;
$flattened_array[ $flattened_key ] = process_data( $item, $flattened_key );
}
return $flattened_array;
}

convert to nested format php

I just want some help in displaying the correct structure of the image data below. Basically what i want is to display it in nested form. From the data below, I would like it to display like this:
Engel (parent which parent_id = 0)
Chest Fridge - Freezers
Small Engel
digital platinum
Upright Fridge
Built-in fridge
Fridge Accessories
Blankets
Carry handles
I hope anyone can help me. Im using php fyi.
Do it recursively
// For example, data fetched from database:
$data = [
['id' => 1, 'sku' => 'name1', 'parent_id' => null],
['id' => 2, 'sku' => 'name12', 'parent_id' => 1],
['id' => 3, 'sku' => 'name12', 'parent_id' => 1],
['id' => 4, 'sku' => 'name2', 'parent_id' => null],
['id' => 5, 'sku' => 'name123', 'parent_id' => 2],
];
// Recursive function
function outTree($data, $parentId = null) {
$str = '';
foreach ($data as $row) {
if ($row['parent_id'] == $parentId) {
$str .= '<li>' . $row['sku'];
$str .= outTree($data, $row['id']);
$str .= '</li>';
}
}
if ($str) {
$str = '<ul>' . $str . '</ul>';
}
return $str;
}
echo outTree($data);

Flatten multidimensional array concatenating keys [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP convert nested array to single array while concatenating keys?
Get array's key recursively and create underscore seperated string
Please, read the whole question before answering.
I have this multidimensional array:
$data = array(
'user' => array(
'email' => 'user#example.com',
'name' => 'Super User',
'address' => array(
'billing' => 'Street 1',
'delivery' => 'Street 2'
)
),
'post' => 'Hello, World!'
);
I want it flatten, transformed into:
$data = array(
'user.email' => 'user#example.com',
'user.name' => 'Super User',
'user.address.billing' => 'Street 1',
'user.address.delivery' => 'Street 2',
'post' => 'Hello, World!'
);
Important:
The keys are very important to me. I want them concatenated, separated by periods.
It should work with any level of nesting.
Thank you!
Something like this should work:
function flatten($array, $prefix = '') {
$result = array();
foreach($array as $key=>$value) {
if(is_array($value)) {
$result = $result + flatten($value, $prefix . $key . '.');
}
else {
$result[$prefix . $key] = $value;
}
}
return $result;
}
DEMO
Thanks for all the given answers.
I have transformed it in the following, which is an improved version. It eliminates the need of a root prefix, does not need to use references, it is cleaner to read, and it has a better name:
function array_flat($array, $prefix = '')
{
$result = array();
foreach ($array as $key => $value)
{
$new_key = $prefix . (empty($prefix) ? '' : '.') . $key;
if (is_array($value))
{
$result = array_merge($result, array_flat($value, $new_key));
}
else
{
$result[$new_key] = $value;
}
}
return $result;
}
Try this
<?php
$data = array(
'user' => array(
'email' => 'user#example.com',
'name' => 'Super User',
'address' => array(
'billing' => 'Street 1',
'delivery' => 'Street 2'
)
),
'post' => 'Hello, World!'
);
function prefixKey($prefix, $array)
{
$result = array();
foreach ($array as $key => $value)
{
if (is_array($value))
$result = array_merge($result, prefixKey($prefix . $key . '.', $value));
else
$result[$prefix . $key] = $value;
}
return $result;
}
var_dump(prefixKey('', $data));
?>
Outputs
array
'user.email' => string 'user#example.com' (length=16)
'user.name' => string 'Super User' (length=10)
'user.address.billing' => string 'Street 1' (length=8)
'user.address.delivery' => string 'Street 2' (length=8)
'post' => string 'Hello, World!' (length=13)
test this out here
i passed by reference so no need for returns. just hand over the array storage.
$store = array();
function flatten($array,&$storage,$parentKey = ''){
foreach($array as $key => $value){
$itemKey = (($parentKey)? $parentKey.'.':'').$key;
if(is_array($value)){
flatten($value,$storage,$itemKey);
} else {
$storage[$itemKey] = $value;
}
}
}
flatten($data,$store);
var_dump($store);
Use recursion such as this:
function process_data( $data, $parent_key ){
if ( ! is_array( $data ) ){
return $data;
}
$flattened_array = array();
foreach( $data as $key => $item ){
$flattened_key = $parent_key . '.' . $key;
$flattened_array[ $flattened_key ] = process_data( $item, $flattened_key );
}
return $flattened_array;
}

Categories