Array comparison in PHP with Laravel - arrays

I have two arrays,says
$arraySel =array("1","Comment","LongText",
"2","Owners","Smalltext"
"3","Text","SmallText");
$arrayCurrent=array("1","Comment","LongText",
"2","Owners","Smalltext");
I need to compare these two arrays and i want to print the item which is nto exists in $arrayCurrent..I am working with PHP.

use array_diff() function :
$arraySel =array("1","Comment","LongText", "2","Owners","Smalltext", "3","Text","SmallText");
$arrayCurrent=array("1","Comment","LongText", "2","Owners","Smalltext");
$result=array_diff($arraySel,$arrayCurrent);
print_r($result);
output : Array ( [6] => 3 [7] => Text [8] => SmallText )
read more here : https://www.w3schools.com/php/func_array_diff.asp

Related

Pass PHP Array to NodeJS

How do I pass a PHP array to a Nightmare NodeJS script? This is where I'm at:
/*Original array $titles =
Array
(
[0] => title 1 with, a comma after with
[1] => title 2
[2] => title 3
[3] => title 4
)
*/
//PHP
$json_titles = json_encode($titles);
echo $json_titles;
// Output: ["title 1 with, a comma after with","title 2","title 3","title 4"]
// Pass to Nightmare NodeJS script with:
shell_exec('xvfb-run node app.js ' . $json_titles);
//NodeJS app.js:
const getTitles = process . argv[2];
console.log(getTitles)
// Output: [title 1 with, a comma after with,title 2,title 3,title 4]
How do I get the same array in PHP to NodeJS?
As you see below Simon came to the rescue with escapeshellarg. Thanks Simon!
I also ended up having one more step in the Node JS script. I needed to: getTitles = JSON.parse(getTitles);
Change your shell_exec to escape the json like so:
shell_exec('xvfb-run node app.js ' . escapeshellarg($json_titles));
This escapes the double quotes in your JSON so they are passed correctly to node.
You should do this any time you pass variables to the command line, to reduce bugs and security risks.
Edit: as discovered by OP, Node will also have to parse the JSON since it gets the argument as a string. This can be done in the node script as follows:
ParsedTitles = JSON.parse(getTitles);

Cakephp Hash combine concatenate two fields

How can I concatenate two fields inside a select box using cakephp 2.5 and Hash:combine ?
Today I have name:
$banks = Hash::combine($banks, '{n}.Bank.id', '{n}.Bank.name');
I need name and identifier, I tried this:
$banks = Hash::combine($banks, '{n}.Bank.id', '{n}.Bank.name {n}.Bank.identifier');
But it return NULL.
How can I have : name - identifier ?
Also try to concatenate the two fields at the model but could not add an hiffen between name and identifier.
You can provide array’s for both $keyPath and $valuePath. If you do this, the first value will be used as a format string, for values extracted by the other paths:
$result = Hash::combine(
$a,
'{n}.User.id',
array('%s: %s', '{n}.User.Data.user', '{n}.User.Data.name'),
'{n}.User.group_id'
);
/* $result now looks like:
Array
(
[1] => Array
(
[2] => mariano.iglesias: Mariano Iglesias
)
[2] => Array
(
[14] => phpnut: Larry E. Masters
)
)
*/
$result = Hash::combine(
$a,
array('%s: %s', '{n}.User.Data.user', '{n}.User.Data.name'),
'{n}.User.id'
);
/* $result now looks like:
Array
(
[mariano.iglesias: Mariano Iglesias] => 2
[phpnut: Larry E. Masters] => 14
)
*/
See CookBook > Hash for mode details.

CakePHP3.4: Why Request::getHeader() returns an array?

I have the following request:
2017-04-05 06:53:31 Error: Cake\Http\ServerRequest Object
(
...
[_environment:protected] => Array
(
[HTTP_REGISTRATION] => eur73_9lhfQ:...SJMryPxCNzKcqSufdpFMOaux
...
)
[_detectorCache:protected] => Array
(
[ajax] =>
[get] => 1
[head] =>
[options] =>
[post] =>
)
[uri:protected] => Zend\Diactoros\Uri Object
(
[allowedSchemes:protected] => Array
(
[http] => 80
[https] => 443
)
...
)
...
)
$this->request->header['registration'] returns:
eur73_9lhfQ:...SJMryPxCNzKcqSufdpFMOaux
as string
$this->request->getHeader('registration') returns:
[Registration] => Array
(
[0] => eur73_9lhfQ:...SJMryPxCNzKcqSufdpFMOaux
)
Why does it return an array?
The HTTP standard allows multiple headers with the same field name to be present in case all values of that header can be expressed as a single, comma separated string (Set-Cookie is an exception to that rule, it cannot express multiple values in a single string, but in practice does still appear multiple times in order to define multiple cookies).
In order to support this in a convenient way, the PSR-7 standard (which \Cake\Http\ServerRequest complies to) defines the getHeader() method to return header values as arrays. The complement to that is the getHeaderLine() method, which is ment to return multiple values as a single, comma separated string.
See also
https://www.rfc-editor.org/rfc/rfc7230#section-3.2.2
http://www.php-fig.org/psr/psr-7/#headers-with-multiple-values

Yii2 Array sorting

In my Yii2 Project I have an array for example
$array = [];
$array [] = 8 , 3, 6
So when I print out the array is
[8,3,6]
So when I use the same in a where statement it jumbles up.
$class = ModelClass::find()->where(['array_no' => $array])->all
So when I print out class I get the output in asc order sorted..
I get the information of
3 in the first
6 in the second place
8 in the third place.
How can i stop this from happening. I want them to return my output in the same order as array
You should use ORDER BY FIELD(), e.g. :
$models = ModelClass::find()
->where(['array_no' => $array])
->orderBy(new \yii\db\Expression('FIELD (array_no, '.implode(',', $array).')'))
->all();

smarty get offset of array

I have some trouble in prestashop 1.6 using smarty.
I have an array, but its offset are not reset for each product.
so for the firdst product, with attrivute it has offset 1,2,3,4 Then for the next product it has offest 5,6,7,8 etc.
I have that kind of array
$combinations Smarty_Variable Object (3)
->value = Array (4)
5 => Array (14)
attributes_values => Array (1)
1 => "S"
attributes => Array (1)
0 => 1
price => 0
specific_price => Array (0)
ecotax => 0
weight => 0
quantity => 20
reference => ""
unit_impact => 0
minimal_quantity => "1"
date_formatted => ""
available_date => ""
id_image => -1
list => "'1'"
6 => Array (14)
I try to go trhough this array but it does not work when I put empty offset (it is inside a foreach)
{$combinations[]['quantity']}
How can I tell him to go trhough the first iteration, and then in the second automatically ?
This return to me the following errors.
Fatal error: Cannot use [] for reading in /htdocs/tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 584
I can not tell him which offset to use, because for each product it goes up and is not reset to 0.
I would be very gratefull for anykind of help.
Here's how to do it, current return the first value of an array
{$combination = current($combinations)}
{$combination['quantity']}
in addition to #UnLoCo answer, if you need these keys 1,2 ... 7,8
{foreach from=$array key=key item=value}
{$key} => {$value}
{/foreach}
or
{foreach $array $key=>$value} {* like PHP style *}
{$key} => {$value}
{/foreach}
Also Smarty docs may help you http://www.smarty.net/docs/en/language.function.foreach.tpl

Resources