$input = "hello|world|look|at|this";
$explode = explode("|", $input);
$array = array("Title" => "Hello!", "content" => $explode);
This will output:
array(2) {
["Title"]=>
string(6) "Hello!"
["content"]=>
array(5) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
[2]=>
string(4) "look"
[3]=>
string(2) "at"
[4]=>
string(4) "this"
}
}
But I want them to be keys with a NULL as value as I add values in a later step.
Any idea how to get the explode() function to return as keys? Is there a function from php available?
array_fill_keys can populate keys based on an array:
array_fill_keys ($explode, null);
Use a foreach loop on the explode to add them:
foreach($explode as $key) {
$array["content"][$key] = "NULL";
}
how about array_flip($explode)? That should give you this
array(2) {
["Title"]=>
string(6) "Hello!"
["content"]=>
array(5) {
[hello]=> 1
No nullvalues but atleast you got the keys right
$input = "hello|world|look|at|this";
$explode = explode('|', $input);
$nulls = array();
foreach($explode as $x){ $nulls[] = null; };
$array = array("Title" => "Hello!", "content" => array_combine($explode, $nulls));
Related
I need the data in the string [$str] that is within quotes to not split.
In this case, "Accounting company" should be kept in one string, not spread.
<?php
$str =
'#PROGRAM "Accounting company" 98.2
#GENERATED 2020715 "SE"';
$data = explode("\n", $str);
foreach($data as &$value){
$value = preg_split("/\s+/", $value);
}
var_dump($data);
Result:
array(2) {
[0]=>
array(4) {
[0]=>
string(8) "#PROGRAM"
[1]=>
string(11) ""Accounting" // Unwanted split
[2]=>
string(8) "company"" // Unwanted split
[3]=>
string(4) "98.2"
}
[1]=>
&array(4) {
[0]=>
string(0) ""
[1]=>
string(10) "#GENERATED"
[2]=>
string(7) "2020715"
[3]=>
string(4) ""SE""
}
}
Wanted result:
array(2) {
[0]=>
array(4) {
[0]=>
string(8) "#PROGRAM"
[1]=>
string(18) ""Accounting company"
[2]=>
string(4) "98.2"
}
[1]=>
&array(4) {
[0]=>
string(0) ""
[1]=>
string(10) "#GENERATED"
[2]=>
string(7) "2020715"
[3]=>
string(4) ""SE""
}
}
You could use a SKIP FAIL pattern to skip matching values from an opening till closing double quote and then match 1+ horizontal whitespace chars to split on
"[^"]*"(*SKIP)(*FAIL)|\h+
Regex demo
$str =
'#PROGRAM "Accounting company" 98.2
#GENERATED 2020715 "SE"';
$data = explode("\n", $str);
foreach($data as &$value){
$value = preg_split("/\"[^\"]*\"(*SKIP)(*FAIL)|\h+/", $value);
}
print_r($data);
Output
Array
(
[0] => #PROGRAM
[1] => "Accounting company"
[2] => 98.2
)
Array
(
[0] =>
[1] => #GENERATED
[2] => 2020715
[3] => "SE"
)
If you don't want the empty entry in the second array, you could use the PREG_SPLIT_NO_EMPTY flag:
$value = preg_split("/\"[^\"]*\"(*SKIP)(*FAIL)|\h+/", $value, -1, PREG_SPLIT_NO_EMPTY);
Php demo
Here a solution without regex
$str =
'#PROGRAM "Accounting company" 98.2
#GENERATED 2020715 "SE"';
$quoted = false;
$index = 0;
$data = [];
$rows = explode("\n", $str);
foreach($rows as $row) {
$temp = [];
for ($i = 0; $i < strlen($row); $i++) {
if ($row[$i] === "\"") $quoted = !$quoted;
if ($row[$i] === " " && !$quoted) {
$index++;
continue;
}
$temp[$index] = ($temp[$index] ?? "") . $row[$i];
}
$data[] = array_values($temp);
}
var_dump($data);
Result
array(2) {
[0]=>
array(3) {
[0]=>
string(8) "#PROGRAM"
[1]=>
string(20) ""Accounting company""
[2]=>
string(4) "98.2"
}
[1]=>
array(3) {
[0]=>
string(10) "#GENERATED"
[1]=>
string(7) "2020715"
[2]=>
string(4) ""SE""
}
}
Demo
Still figuring out a regex solution though :)
In case you want to keep the empty element at [1][0]: Demo
I have an array $arrItems['items'] in which 5 more arrays (associate array) and each array contain 5 element (with the keys: f_name, l_name, contact, address, seller_id).
I want to get all those arrays (from $arrItems['items']) in which element of seller_id is 1 like "seller_id"=>"1,2,3" OR "seller_id"=>"3,2,1" OR "seller_id"=>"4,6,2" OR "seller_id"=>"5,3,4" OR "seller_id"=>"2,1,2" Array given below.
array(5)
{
[0] =>
array(5)
{
["f_name"] =>
string(3) "abc"
["l_name"] =>
string(3) "xyz"
["contact"] =>
string(5) "12345"
["address"] =>
string(3) "xyz"
["seller_id"] =>
string(1) => "1,2,3"
}
[1]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"3,2,1"
}
[2]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"4,6,2"
}
[3]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"5,3,4"
}
[4]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"2,1,2"
}
Kindly Help me it actually order table i want to just pick those array in which current seller id is 1 . for example seller No # 1 is login. then all those array select mean those array save in other array.
array_filter (documentation) with in_array (documentation):
$sellerId = "1";
$arr = array_filter($arrItems['items'], function($e) use ($sellerId) {
return in_array($sellerId, explode(",", $e["seller_id"]);
});
If you want only those 5 option use:
$options = array("1,2,3", "3,2,1", "5,3,4", "4,6,2", "2,1,2");
$arr = array_filter($arrItems['items'], function($e) use ($options ) {
return in_array($e["seller_id"], $options);
});
Edited: As for your request this is version of the same code using foreach loop:
$sellerId = "1";
$seller_order_arr = [];
foreach ($arrItems['items'] as $row) {
if (in_array($sellerId, explode(",", $row["seller_id"])))
$seller_order_arr[] = $row;
}
Now $seller_order_arr will hold your filtered array
If I see that correctly, your seller_id value is a concatenated string of Ids. You can filter your outer array with array_filter and a custom callback function, which should return true, if the element should be kept.
$idToLookup = 1;
$filteredItems = array_filter($arrItems['items'], function($element) {
return preg_match('/(^|,)' . $idToLookup . '($|,)/', $element['seller_id']);
]);
Or if you are not so familiar with regex, first explode the number-string into individual numbers and then use in_array:
$idToLookup = 1;
$filteredItems = array_filter($arrItems['items'], function($element) {
$sellerIds = explode(',', $element['seller_id']);
return in_array($idToLookup, $sellerIds);
]);
Use array_filter()
$array = array_filter($arrItems['items'], function ($item) {
$ids = explode(',', $item['seller_id']);
return in_array(1, $ids);
});
I have a PHP array, which looks like follows:
array(8) {
[0]=>
string(3) "639"
[1]=>
string(2) "33"
[2]=>
string(2) "68"
[3]=>
string(3) "196"
[4]=>
string(3) "275"
[5]=>
string(3) "309"
[6]=>
string(3) "331"
[7]=>
string(3) "378"
}
I would like to change all the keys to these values to incrementing letters (a, b, c, etc.) - how would I do this?
I realise I can increment letters like so:
$x = "a";
$x++;
echo $x;
"b"
But how can I perform this within a loop?
The desired result would be something like this:
"a" => "639"
"b" => "33"
"c" => "68"
etc.
I think the following can help
$newArray = array();
$index = "a";
foreach($oldArray as $value)
{
$newArray[$index] = $value;
$index++;
}
You have provided the answer pretty much yourself already
$array = array('639', '33', '68', '196', '275', '309', '331', '378');
$index = 'a';
$newArray = array();
foreach ($array as $value) {
$newArray[$index++] = $value;
}
Following code will surely help you:
$result = [];
array_walk($data,function($v,$k)use (&$result){
$result[chr(65 + $k)] = $v;
});
print_r($result);
Demo
Please I have this string :
$s = "Cannes (06150-06400), Limoges (87000-87100-87280), 06000, Paris";
I want to store only words in an array, so I tried this :
$villes = explode(',', preg_replace('#\(([0-9\-]*)\)#', '', $s));
But in result I get this array :
array(4) {
[0]=>
string(7) "Cannes "
[1]=>
string(9) " Limoges "
[2]=>
string(7) " 06000" // This shouldn't be displayed in the array
[3]=>
string(6) " Paris"
}
Please how could I modify the regex to get it work as I wish. Thanks.
Try:
<?php
$s = "Cannes (06150-06400), Limoges (87000-87100-87280), 06000, Paris";
preg_match_all("/[a-zA-Z]+/", $s, $villes);
var_dump($villes[0]);
?>
For single word names.
Var dump:
array(3) {
[0]=>
string(6) "Cannes"
[1]=>
string(7) "Limoges"
[2]=>
string(5) "Paris"
}
Try
$arr = preg_split('/\s*\(?[\d\-\s]*\)?\s*,\s*/', $s, -1, PREG_SPLIT_NO_EMPTY);
var_dump($arr);
Output
array(3) {
[0] =>
string(6) "Cannes"
[1] =>
string(7) "Limoges"
[2] =>
string(5) "Paris"
}
preg_match( '/[a-zA-Z\-\']+/', $s, $words );
var_dump( $words[0] );
you can use array for result.
$arr=explode(' ' , $s);
print_r($arr);
$arr = array('one' => array('one_1' => array('one_2' => '12')), 'two', 'three');
$arr2 = array('one_2' => 'twelve');
$merge = array_merge($arr, $arr2);
print '<pre>';
var_dump($merge);
print '</pre>';
gives:
array(4) {
["one"]=>
array(1) {
["one_1"]=>
array(1) {
["one_2"]=>
string(2) "12"
}
}
[0]=>
string(3) "two"
[1]=>
string(5) "three"
["one_2"]=>
string(6) "twelve"
}
I want the value of key one_2 in the first array to be replaced with the value of the same key in the second array. So the result would be:
array(4) {
["one"]=>
array(1) {
["one_1"]=>
array(1) {
["one_2"]=>
string(2) "twelve"
}
}
[0]=>
string(3) "two"
[1]=>
string(5) "three"
}
array_walk_recursive($arr, function (&$value, $key, $replacements) {
if (isset($replacements[$key])) {
$value = $replacements[$key];
}
}, $arr2);
Note that this uses PHP 5.3+ syntax.