How to add extra values to a existing array - php

How to add extra values to a existing array
$item = get_post_meta($post->ID, 'extra_fileds', true);
when I print $item I get the following
Array
(
[0] => Array ( [name] => test1 [type] => this1 [location] => 1 )
[1] => Array ( [name] => test2 [type] => this2 [location] => 2 )
)
I would like to add a extra field and make it like`
Array
(
[0] => Array ( [name] => test1 [type] => this1 [location] => 1 )
[1] => Array ( [name] => test2 [type] => this2 [location] => 2 )
[2] => Array ( [name] => test3 [type] => this3 [location] => 3 )
)
Thank you in advance

Write
$item[] = ['name'=>'test3','type'=>'this3','location'=>3];

Here either you can use array_push or $rows[] will solve your problem.
Try this code snippet here
ini_set('display_errors', 1);
$rows=Array (
0 => Array ( "name" => "test1","type" => "this1", "location" => 1 ),
1 => Array ( "name" => "test2" ,"type" => "this2", "location" => 2 ) );
$arrayToAdd=Array ( "name" => "test3","type" => "this3", "location" => 3 );
Solution 1:
array_push($rows, $arrayToAdd);
Solution 2:
$rows[]=$arrayToAdd;

Use array push.
$new_array_item=array("name" => "test3","type" => "this3", "location" => 3);
array_push($item, $new_array_item);
print_r($item);

You're array is at the moment stored in $item.
To add a new item use these brackets: [ ].
Here is your code:
$item[] = [
'name' => 'test3'
'type' => 'this3'
'location' => 3
]
You can use this as much as you like to add more items.
I think that this is the best solution but you can also take a look at php array_push() function.

Related

Change order of multidimensional array columns

I have a multidimensional array in php and i need to change the column order with the order of a second simple array.
EDIT:
Although both arrays are the same in regard of values and keys, im using this for export with phpexcel, and it generates the xls file with the order of the given array. I need to change that order so the xls file looks that way.
The array looks like this:
Array
(
[0] => Array
(
[name] => Name1
[sn] => Sn1
[somenumber] => 43234234
)
[1] => Array
(
[name] => Name2
[sn] => Sn2
[somenumber] => 4564564
)
[2] => Array
(
[name] => Name3
[sn] => Sn3
[somenumber] => 6575647456745
)
)
And the second array is this:
Array
(
[0] => sn
[1] => name
[2] => somenumber
)
What i need is the first array to be ordered based on the second so it looks like this:
Array
(
[0] => Array
(
[sn] => Name1
[name] => Sn1
[somenumber] => 43234234
)
[1] => Array
(
[sn] => Name2
[name] => Sn2
[somenumber] => 4564564
)
[2] => Array
(
[sn] => Name3
[name] => Sn3
[somenumber] => 6575647456745
)
)
this is how you can sort your array:
$template array:
//template array
$reference = array('sn', 'name', 'somenumber');
$array_to_sort = Array
(
"0" => Array
(
"somenumber" => "Name1",
"sn" => "Sn1",
"name" => "43234234"
),
"1" => Array
(
"sn" => "Name2",
"somenumber" => "4564564",
"name" => "Sn2"
),
"2" => Array
(
"sn" => "Name3",
"name" => "Sn3",
"somenumber" => "6575647456745"
)
);
$ordered_array = [];
foreach ($array_to_sort as $key => $value) {
$ordered_array[] = array_replace(array_flip($reference), $value);
}
print_r($ordered_array);
If all keys always present
// Make template array with correct order of keys
$template = array_flip($second);
foreach($array as &$x) {
// replace values in template
$x = array_replace($template, $x);
}
demo

Fetch data from 2nd array and put values in the array 1

I have the following situation and cannot find an easy way to do it.
I have 2 arrays:
1)
Array (
[0] => Array
(
[FirstName] => Tom
[LastName] => Siemens
[Id] => 10300
)
[1] => Array
(
[FirstName] => Sam
[LastName] => Tailor
[Id] => 10301
)
2)
Array
(
[0] => Array
(
[Type] => Invoice
[Number] => 6344394
[Project] => Array
(
[Name] => Test Project 1
[ResponsibleUserId] => 10300
[Id] => 498
[ResponsibleUser] =>
)
)
[1] => Array (
[Type] => Invoice
[Number] => 6345555
[Project] => Array
(
[Name] => Test Project 2
[ResponsibleUserId] => 10301
[Id] => 499
[ResponsibleUser] =>
)
)
What could be the best approach to get the "FirstName LastName" from the first array depends on the ID which must equal the Project Id from the second array and to put these values inside the second array -> Project -> ResponsibleUser?
The result I'm looking for is the following:
Final Array)
Array (
[0] => Array
(
[Type] => Invoice
[Number] => 6344394
[Project] => Array
(
[Name] => Test Project 1
[ResponsibleUserId] => 10300
[Id] => 498
[ResponsibleUser] => Tom Siemens
)
)
[1] => Array (
[Type] => Invoice
[Number] => 6345555
[Project] => Array
(
[Name] => Test Project 2
[ResponsibleUserId] => 10301
[Id] => 499
[ResponsibleUser] => Sam Tailor
)
)
If you start by indexing the first array by the id using array_column() ...
$idList = array_column($array1, null, "Id");
You can then just loop over the second array and pick out the name each time and update it...
foreach (array2 as &$project ) {
$id = $project["Project"]["ResponsibleUserId"];
$project["Project"]["ResponsibleUser"] = $idList[$id]["FirstName"]." ".$idList[$id]["LastName"];
}
Use &$project as this allow you to update the original value which is what your after.
Create a new array from array1 with the user id as the keys:
$newArray1 = array();
foreach($array1 as $values)
{
$newArray1[$values['Id']] = $values['FirstName']." ".$values['LastName'];
}
Then
foreach($array2 as $index => $val)
{
$array2[$index]['Project']['ResponsibleUser'] = $newArray1[$val['Project']['ResponsibleUserId'];
}
<?php
$names[0] = array("FirstName" => "Tom", "LastName" => "Siemens", "Id" => 10300);
$names[1] = array("FirstName" => "Sam", "LastName" => "Tailor", "Id" => 10301);
$projects[0] = array("Type" => "Invoice", "Number" => "6344394", "Project" => Array("Name" => "Test Project 1", "ResponsibleUserId" => 10300, "Id" => 498, "ResponsibleUser" => ""));
$projects[1] = array("Type" => "Invoice", "Number" => "6345555", "Project" => Array("Name" => "Test Project 2", "ResponsibleUserId" => 10301, "Id" => 499, "ResponsibleUser" => ""));
foreach ($projects as $pid => $project) {
foreach ($names as $nid => $name) {
if ($project["Project"]["ResponsibleUserId"] == $name["Id"]) {
$projects[$pid]["Project"]["ResponsibleUser"] = $name["FirstName"].' '.$name["LastName"];
};
};
};
print_r($projects);
?>

How to group posted array names by index in PHP

I am currently working on a project where the fields scale when clicking on the "Add" button.
I am grouping each field like this: name="packaging[]", name="packaging[1]", name="packaging[2]" and so on. When I submit the form, this is how the data looks like when posted:
Array
(
[packaging] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 2
)
[total-weight] => Array
(
[0] => 1
[1] => 2
)
[length] => Array
(
[0] => 1
[1] => 2
)
)
Using PHP I would like to convert the above code to look like this:
Array
(
[0] => Array
(
[packaging] => 1,
[quantity] => 1,
[total-weight] => 1,
[length] => 1,
)
[1] => Array
(
[packaging] => 2,
[quantity] => 2,
[total-weight] => 2,
[length] => 2,
)
)
Any help would be greatly appreciated.
Try this....
$array=array();
foreach($data as $key=>$value){
foreach($value as $k=>$val){
$array[$k][$key]=$val;
}
}
DEMO
Try this code:
$rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)
);
$res_array = array();
$total_records = count($rows['packaging']);
for($i=0;$i<$total_records;$i++)
{
$res_array[] = array('packaging'=>$rows['packaging'] [$i],'quantity'=>$rows['quantity'][$i],
'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
}
print_r($res_array);

Get array values having same key from one array and store those values in another array

I am working on a php project. I am stuck at this point.
Here is the array that I have.
[text_numeric] => Array
(
[text] => Numeric field fillable by user
[parameters] => Array
(
[prefix] => 1
[price] => 1
[sku] =>
[quantity] =>
[weight] =>
[min_value] => 1
[max_value] => 1
)
[operand] => Array
(
[op_fix_discount] => 1
[op_fix_recharge] => 1
[op_per_unit] => 1
[op_percentage] =>
)
)
[checkbox] => Array
(
[text] => Checkbox attributes
[parameters] => Array
(
[prefix] => 1
[price] => 1
[sku] => 1
[quantity] => 1
[weight] => 1
[min_value] =>
[max_value] =>
)
[operand] => Array
(
[op_fix_discount] => 1
[op_fix_recharge] => 1
[op_per_unit] =>
[op_percentage] => 1
)
)
I want to get all the values from this array with key value "text" in another array.
Like this:
Array
(
[0]=>Numeric field fillable by user
[1]=>Checkbox attributes
)
It could have been great if you had show us your efforts to achieve that but since you are new here is the codez. You can simply get it using a foreach loop,
$new_array = array();
foreach($your_array as $k=>$row){
$new_array[$k] = $row['text'];
}
print_r($new_array);
You can also use array_map,
function getTextField($a) {
return $a['text'];
}
$texts = array_map('getTextField', $your_array);
print_r($texts);

push certain key/value into existing array

I'm trying to put data into from:
[comments] => Array
(
[count] => 2
[data] => Array
(
[0] => Array
(
[idcomments] => 1
[from] => Array
(
[idusers] => 1
[username] =>
[full_name] => AndrewLiu
)
[text] => testing this comment out
)
[1] => Array
(
[idcomments] => 2
[from] => Array
(
[idusers] => 1
[username] =>
[full_name] => AndrewLiu
)
[text] => more comments yeah
)
)
)
I have something like this:
while($row = $SQL_products -> fetch(PDO::FETCH_ASSOC)){
$json['data'][] = array(
"comments" =>array(
"count"=>$SQL_ccount[0],
"data" => array($SQL_comments->fetchAll(PDO::FETCH_ASSOC),
"from" => array($SQL_comments_from->fetchAll(PDO::FETCH_ASSOC)))
),
);
}
But it doesn't fall into the from. Not sure what the right syntax is to get $SQL_comments_from into data such like the example above.
This is what I'm getting
[comments] => Array
(
[count] => 2
[data] => Array
(
[0] => Array
(
[0] => Array
(
[idcomments] => 1
[from] => 1
[text] => testing this comment out
)
[1] => Array
(
[idcomments] => 2
[from] => 2
[text] => more comments yeah
)
)
[from] => Array
(
[0] => Array
(
[idusers] => 1
[username] =>
[full_name] => AndrewLiu
)
)
)
)
I'm trying to get "from" into the "data". The example I provided does not make the "from" go into the other "from" (thats right under idcomments)
Thanks in advance!
Your data field uses data from SQL_comments_from and $SQL_comments_from mixed together, this should generate what you want.
while($row = $SQL_products -> fetch(PDO::FETCH_ASSOC)){
$from = $SQL_comments_from->fetchAll(PDO::FETCH_ASSOC);
$data= array();
foreach ($SQL_comments->fetchAll(PDO::FETCH_ASSOC as $items){
$data[] = array('idcomments' => $items['idcomments'], 'from' => $from[$items['from']], 'text' => $items['text']);
}
$json['comments'][] = array(
"count"=>$SQL_ccount[0],
"data" => $data;
)
),
);
}

Categories