I've an array titled $val. It's a blank array. I want it in following manner i.e. after print_r($val); it should print the array $val in following manner:
I've following variables too :
$_POST['fileName'] = 'Sample_1.docx';
$_POST['fileLink'] = 'https://www.filepicker.io/api/file/HEYQ6lPNRg2lzjUNhUGX';
Array
(
[status_info] =>
[user_status] => Share what's going on...
[group_id] =>
[action] => upload_photo_via_share
[is_activity_feed] => 1
[vshare] => Array
(
[Sample_1.docx] => Array
(
[0] => https://www.filepicker.io/api/file/HEYQ6lPNRg2lzjUNhUGX
)
)
[link] => Array
(
[url] => http://
)
[poll_question] =>
[answer] => Array
(
[0] => Array
(
[answer] =>
)
[1] => Array
(
[answer] =>
)
)
[is_profile] => no
[iframe] => 1
[method] => simple
[document_title] => document
[category] => Array
(
[0] =>
)
[code_snippet] =>
)
I want to add following structure to the variable $val. This thing should be done using PHP code only.
How should I add following structure of key-values to the array $val in PHP?
Thanks in advance.
The PHP source of this array should be like this:
$val = array(
'status_info' => '',
'user_status' => 'Share what's going on...',
...
'vshare' => array(
'Sample_1.docx' => array (
'https://www.filepicker.io/api/file/HEYQ6lPNRg2lzjUNhUGX'
)
),
'link' => array(
'url' => 'http://'
),
...
);
Related
I'm using print_r(array_unique($array, SORT_REGULAR)); on the array below but it does not work.
I'm trying to filter out the redundant data.
Notice that [Order] and its key value pairs are all the same. But [Transaction] and its key value pairs are unique.
I need to get the [Order] element data and combine it with the 3 different [Transaction] elements.
My array
Array
(
[0] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
)
[1] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
)
[2] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
The final array I need will look something like this.
Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[0] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
I can flatten the original array and then use array_unique, but wanted to see if there is a better way to accomplish what I need.
my code:
$myarray = array(
0 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11211", "TransactionPrice" => 91.17)
),
1 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11212", "TransactionPrice" => 180.41)
),
2 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11213", "TransactionPrice" => 209.99)
)
);
print_r(array_unique($myarray, SORT_REGULAR));
If you want to determine how many unique values of the Order element there are in your array, you need to apply array_unique only to the Order elements, which you can do using array_column:
$unique_orders = count(array_unique(array_column($myarray, 'Order'), SORT_REGULAR));
You can process your array using a list of keys which have non-unique values to generate an array, while other keys will have just a single value:
$non_unique_keys = ['Transaction'];
$output = array();
foreach (array_keys($myarray[0]) as $key) {
if (in_array($key, $non_unique_keys)) {
$output[$key] = array_column($myarray, $key);
}
else {
$output[$key] = $myarray[0][$key];
}
}
print_r($output);
Example Output:
Array (
[Order] => Array (
[PO] => TR11214
[OrderID] => 242856952012
)
[Sales Tax] => Array (
[PO] => TR11214
[SalesTaxAmount] => 0
)
[Transaction] => Array (
[0] => Array (
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array (
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array (
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
Demo on 3v4l.org
array_unique() is intended for single dimensional arrays. If you want to use it on a multi-dimentional array, you should consider using usort() instead. Then you'll need to iterate through the array in reverse manually, searching for duplicates and removing them.
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.
I am calling a PHP file using http.post, passing a json object in the process.
I have managed to retrieve the object from within the PHP and have attached the dump below. All I now need is to retrieve 'name', 'email' and 'message' strings from the array but am finding this difficult as not used to PHP.
Connected successfully<pre>string(2467) "Array
(
[name] => Array
(
[$viewValue] => testing one two
[$modelValue] => testing one two
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => fullName
[$options] =>
)
[email] => Array
(
[$viewValue] => test#onetwo.com
[$modelValue] => test#onetwo.com
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => email
[$options] =>
)
[message] => Array
(
[$viewValue] => testing testing
[$modelValue] => testing testing
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => message
[$options] =>
)
)
"
<br /><br />Array
(
[name] => Array
(
[$viewValue] => testing one two
[$modelValue] => testing one two
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => fullName
[$options] =>
)
[email] => Array
(
[$viewValue] => test#onetwo.com
[$modelValue] => test#onetwo.com
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => email
[$options] =>
)
[message] => Array
(
[$viewValue] => testing testing
[$modelValue] => testing testing
[$validators] => Array
(
)
[$asyncValidators] => Array
(
)
[$parsers] => Array
(
)
[$formatters] => Array
(
[0] =>
)
[$viewChangeListeners] => Array
(
)
[$untouched] =>
[$touched] => 1
[$pristine] =>
[$dirty] => 1
[$valid] => 1
[$invalid] =>
[$error] => Array
(
)
[$name] => message
[$options] =>
)
)
</pre>
The PHP code which retrieves the object in the first place is as follows:
$data = json_decode(file_get_contents('php://input'), TRUE);
$text = print_r($data,true);
echo "<pre>";
var_dump($text);
echo "<br /><br />";
print_r($text);
echo "</pre>";
How can I access the 'name', 'email' and 'message' strings please?
First of all your array isnt a valid one... it should be something like this:
BTW remove the $ from inside the arrays.
$newarr = Array(
"name" => Array(
"first_name" => "Alex",
"last_name" => "Gonzalez"
),
"email" => Array(),
"other_sub_array" => Array()
);
Now to get let say the first name, since it is a subarray (so an array inside another array).
echo $newarr['name']['first_name'];
// Result: Alex
Hope this helps.
UPDATE
I didn't mean not valid, it is a bad practice to use an array like that.
Your array is valid unlike the other answer supposes. I assume that you are using some framework that generates the relatively strange json you're reading in. But to the point you can get them via:
//note I used single quotes so that the dollar sign isn't evaluated to a php variable
$data['name']['$modelValue'];
$data['message']['$modelValue'];
$data['email']['$modelValue'];
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;
)
),
);
}
Please see my DEMO and answer my question: why date in values html on offset 2 not as date and it is a number?
DEMO: http://codepad.viper-7.com/r9FYnb
$data = array();
$data_1 = $_POST['data_1'];
$static = $_POST["static"];
foreach($static as $idx=>$val){
$data[] = array(
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx]*2+1)),
'static' => $static[$idx]
);
}
This is output:
Array
(
[0] => Array
(
[data_1] => ["2011\/8\/02",4023] **//4023 !?**
[static] => 12
)
[1] => Array
(
[data_1] => ["2011\/8\/09",4023] **// 4023!?**
[static] => 34
)
[2] => Array
(
[data_1] => ["2011\/8\/16",4023] **// 4023 !?**
[static] => 56
)
)
I'm not certain what you are trying to do here, but I see an inconsistency between how you manipulate $idx
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx]*2+1)),
// -----^^^^^^^^-------^^^^^^^^^^^^
For the second offset, perhaps you intend to modify $idx inside the []
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[($idx*2)+1])),
// ---------------------^^^^^^^^^^^^
Sample output after modifying your demo:
Array
(
[0] => Array
(
[data_1] => ["2011\/8\/02","2011\/8\/08"]
[static] => 12
)
[1] => Array
(
[data_1] => ["2011\/8\/09","2011\/8\/15"]
[static] => 34
)