print array values in new format in array - php

I want to send the value to jquery-variable in new format, i am using nested foreach loop
here is my code:
foreach($names as $key1 => $desc1) {
$arr_names[] = $names;
$details = get_details( $key1 );
if ( is_array( $states ) ) {
foreach($details as $key2 => $desc2) {
$title[] = $key2;
$value[] = $desc2;
}
}
}
echo "<input type='hidden' id='storageElement' data-storectrystts='".json_encode($namdetailsArray)."'>";
Right now, i can get this:
Array
(
[0] => Sam
[1] => Ben
[2] => John
[0] => Age
[1] => Place
[2] => Height
[3] => Weight
[4] => Year
[5] => Salary
[0] => 30
[1] => AU
[2] => 6
[3] => 150
[4] => First
[5] => 50000
)
I need to send value in this format through data-storectrystts='".json_encode($namdetailsArray).":
'Sam' => [
['Age', '30'],
['Place', 'AU']],
'Ben' => [
['Height', '6'],
['Weight', '150']],
'John' => [
['Year', 'First'],
['Salary', '50000']]
Update
//if we use print_r($namdetailsArray); then everything is fine
Array
(
[Sam] => Array
(
[0] => Array
(
[0] => Age
[1] => 30
)
[1] => Array
(
[0] => Place
[1] => AU
)
)
[Ben] => Array
(
[0] => Array
(
[0] => Height
[1] => 6
)
[1] => Array
(
[0] => Weight
[1] => 150
)
)
[John] => Array
(
[0] => Array
(
[0] => Year
[1] => First
)
[1] => Array
(
[0] => Salary
[1] => 50000
)
)
[Derek] => Array
(
[0] => Array
(
[0] => tax's cal
[1] => 100
)
[1] => Array
(
[0] => distance
[1] => 5
)
)
)
//if we use echo json_encode($namdetailsArray) then everything is fine
{"Sam" => [
["Age", "30"],
["Place", "AU"]],
"Ben" => [
["Height", "6"],
["Weight", "150"]],
"John" => [
["Year", "First"],
["Salary", "50000"]],
"Derek" => [
["tax's cal", "100"],
["distance", "5"]]}
/*but if we use
echo "<input type='hidden' id='storageElement' data-storectrystts='".json_encode($namdetailsArray)."'>";
echo '<div id="availhai"></div>';
var cSttArry = $("#storageElement").data('storectrystts');
$("#availhai").html(cSttArry);
then it is not showing anything after word "tax" */
{"Sam" => [
["Age", "30"],
["Place", "AU"]],
"Ben" => [
["Height", "6"],
["Weight", "150"]],
"John" => [
["Year", "First"],
["Salary", "50000"]],
"Derek" => [
["tax

You shouldn't be creating lots of separate arrays. Create a single associative array where the key is the name, and the value is a 2-dimensional array like you show.
$namdetailsArray = array();
foreach ($names as $name => $desc) {
$personArray = array();
foreach ($desc as $key => $value) {
$personArray[] = array($key, $value);
}
$namdetailsArray[$name] = $personArray;
}

Related

PHP associative array- matched key value of each array will be a table row

I have an array like this-
Array
(
[sku] => Array
(
[0] => SKU125
[1] => SKU121
[2] => SKU122
[3] => SKU124
)
[variation_description] => Array
(
[0] => test another
[1] => test
[2] => test
[3] => test
)
[price_html] => Array
(
[0] => 400,200
[1] => 500
[2] => 600,300
[3] => 700
)
)
Is it possible to covert the array to like this table-
Any kind of help will be appreciated.
Thanks In Advance
As every key (sku , price_html etc) has same amount of data , so just push the corresponding key data to a new array.
$data = [
'sku' => ['SKU125', 'SKU121', 'SKU122', 'SKU124'],
'variation_description' => ['test another', 'test', 'test', 'test'],
'price_html' => ['400,200', '500', '600,300', '700']
];
$re_structured = [];
foreach ($data as $each_key_data ) {
foreach ($each_key_data as $key => $value2 ) {
$re_structured[$key][] = $value2;
}
}
var_dump($re_structured);
You can simply iterate through data and create a new array.
$data = [
'sku' => ['SKU125', 'SKU121', 'SKU122', 'SKU124'],
'variation_description' => ['test another', 'test', 'test', 'test'],
'price_html' => ['400,200', '500', '600,300', '700']
];
$mapped = [];
$keys = array_keys($data);
$rows = count($data[reset($keys)]);
for ($i = 0; $i < $rows; $i++) {
$row = [];
foreach ($keys as $key)
$row[] = $data[$key][$i];
$mapped[] = $row;
}
This will result in
print_r($mapped);
Array
(
[0] => Array
(
[0] => SKU125
[1] => test another
[2] => 400,200
)
[1] => Array
(
[0] => SKU121
[1] => test
[2] => 500
)
[2] => Array
(
[0] => SKU122
[1] => test
[2] => 600,300
)
[3] => Array
(
[0] => SKU124
[1] => test
[2] => 700
)
)

make inline result from array value

I have two array as bellow :
First array :
Array
(
[0] => Array
(
[0] => Array
(
[name] => one
[number] => 051
)
[1] => Array
(
[name] => two
[number] => 052
)
[2] => Array
(
[name] => three
[number] => 053
)
)
[1] => Array
(
[0] => Array
(
[name] => four
[number] => 061
)
[1] => Array
(
[name] => five
[number] => 062
)
)
)
I want to make output from first array above
[0] => 051, 052, 053.
[1] => 061, 062.
Array
(
[0] => Array
(
[0] => Array
(
[name] => book
[number] => 41
)
[1] => Array
(
[name] => pencil
[number] => 42
)
)
[1] => Array
(
[name] => eraser
[number] => 71
)
)
I want to make output from second array above
[0] => 41, 42.
[1] => 71.
Please advise. Thank you.
You can make a try like this way with two foreach() loop.
$numbers = [];
foreach ($array as $k => $v) {
$num = [];
foreach ($v as $k2 => $v2) {
$num[] = $v2['number'];
}
$numbers[$k] = implode(',',$num).'.';
}
print_r($numbers);
DEMO: https://3v4l.org/mEeO7
you can try something like this
$arr = Array (
Array (
Array (
"name" => "one",
"number" => "051"
),
Array (
"name" => "two",
"number" => "052"
),
Array (
"name" => "three",
"number" => "053"
)
),
Array (
Array (
"name" => "four",
"number" => "061"
),
Array (
"name" => "five",
"number" => "062"
)
)
);
foreach ($arr as $k => $s_arr) {
echo "[" . $k . "] => ";
foreach ($s_arr as $k2 => $v2) {
echo $v2["number"] . " ";
}
echo "\n";
}

compare values from multidimensional array and add key to array

Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)

How do I remove one array from another in php?

I have the following arrays in PHP:
print_r($employees) =
Array
(
[0] => Array
(
[p_id] => T29083999
[name] => Robert Plaxo
)
[1] => Array
(
[p_id] => T29083388
[name] => Yvan Sergei
)
[2] => Array
(
[p_id] => T21083911
[name] => Rhonda Saunders
)
[3] => Array
(
[p_id] => H02910382
[name] => Miguel Mercado
)
)
then this array:
print_r($record) =
Array
(
[0] => Array
(
[c_id] => 1
[section] => 1061
[term] => 201631
[p_id] => T29083388
[c_date] => 2016-04-01 09:14:00
)
)
I want to remove the array element from $employees that matches the p_id of $record. Array $record may have multiple entries like the one shown. If so, any p_id in $record must be removed from $employees.
I tried:
foreach ($employees as $k => $e) {
foreach ($record as $r) {
if ($e['p_id']==$r['p_id']) {
echo "found it!";
// if I uncomment the next line, it crashes! (500 server error)
// unset $employees[$k];
}
}
}
I just want to remove any element from $employees that has any employee that matches any record in $record with that employee id.
You were almost there; just needed parens around your unset()
I also took the liberty to change some of your variable names as single character variable names bother me.
$employees[] = [
'p_id' => 'T29083999',
'name' => 'Robert Plaxo',
];
$employees[] = [
'p_id' => 'T29083388',
'name' => 'Yvan Sergei',
];
$employees[] = [
'p_id' => 'T21083911',
'name' => 'Rhonda Saunders',
];
$employees[] = [
'p_id' => 'H02910382',
'name' => 'Miguel Mercado',
];
$records[] = [
'c_id' => '1',
'section' => '1061',
'term' => '201631',
'p_id' => 'T29083388',
'c_date' => '2016-04-01 09:14:00',
];
foreach ($employees as $key => $employee) {
foreach ($records as $record) {
if ($employee['p_id'] == $record['p_id']) {
echo "found it!";
unset($employees[$key]);
}
}
}
echo "<pre>";
print_r($employees);
Outputs
found it!
Array
(
[0] => Array
(
[p_id] => T29083999
[name] => Robert Plaxo
)
[2] => Array
(
[p_id] => T21083911
[name] => Rhonda Saunders
)
[3] => Array
(
[p_id] => H02910382
[name] => Miguel Mercado
)
)
The short solution using array_column and array_filter functions. It will also fit your requirement "Array $record may have multiple entries":
$p_ids = array_column($record, "p_id");
$employees = array_filter($employees, function($v) use($p_ids){
return !in_array($v["p_id"], $p_ids);
});
print_r($employees);
The output:
Array
(
[0] => Array
(
[p_id] => T29083999
[name] => Robert Plaxo
)
[2] => Array
(
[p_id] => T21083911
[name] => Rhonda Saunders
)
[3] => Array
(
[p_id] => H02910382
[name] => Miguel Mercado
)
)

How to merge array field values in php?

My array is like this:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord
)
[1] => Array
(
[id] => 2
[name] => b
[hardware_type] => mouse
)
[2] => Array
(
[id] => 1
[name] => a
[hardware_type] => mouse
)
[3] => Array
(
[id] => 1
[name] => a
[hardware_type] => moniter
)
[4] => Array
(
[id] => 2
[name] =>b
[hardware_type] => keyboad
)
)
required out put like this i want only merge hardware type
Array(
[0] => Array
(
[id] => 1
[name] => a
[hardware_type] => keybord, mouse, moniter
)
[1] => Array
(
[id] => 1
[name] => b
[hardware_type] => keyboard, mouse
)
)
Where $array is the input array you described, where $newarray is the output array you desire, and assuming every value of id has the same name as in your example input:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
if (empty($newarray[$item['id']]['hardware_type']))
$temp[$item['id']]['hardware_type'] = $item['hardware_type'];
else
$temp[$item['id']]['hardware_type'] .= ', ' . $item['hardware_type'];
}
$newarray = array_values($temp);
If you want the hardware_type to be an array instead of a comma-separated list of strings, do this instead:
$temp = array();
foreach ($array as $item) {
$temp[$item['id']] = array('id' => $item['id'], 'name' => $item['name']);
$temp[$item['id']]['hardware_type'][] = $item['hardware_type'];
}
$newarray = array_values($temp);

Categories