Preserve keys after calling to preg_split - php

I'm using preg_match for performing a regular expression match, but before using it I declared my array with specific keys as follows:
$record = array ( "title" => '',
"review" => '',
"publisher" => '',
);
and here I call the function
$record = preg_split( "/(\s\|\s)|(\s\|)/", $data, 4 );
If I try to use the array with
echo $record['title'];
I got the following message error:
PHP Notice: Undefined index: title
Is there a way to preserve my keys?

Related

"Deprecated: parse_str(): Calling parse_str() without the result argument is deprecated in"

I want to run the following command with exec but I am getting an error. So what should I use instead?
php /var/.../../example.php -- 123456789 exampleData1 exampleData2
Error:
Deprecated: parse_str(): Calling parse_str() without the result argument is deprecated in /var/.../../example.php on line X
I tried this:
$argumentsArray = [
'postID' => 123456
'foo' => 'bar'
];
exec(php /var/.../../example.php -- $argumentsArray);
and;
parse_str(parse_url($argv[0], PHP_URL_QUERY),$arguments);
$postID = $arguments['postID'];
Error: Notice: Undefined index: postID in..
You can't substitute an entire array into a string. Use implode() to convert an array into a string with a delimiter between the values.
$argumentsArray = [
'postID' => 123456
'foo' => 'bar'
];
$arguments = implode(' ', $argumentsArray);
exec("php /var/.../../example.php -- {$arguments}");

An effective way to take the appropriate values from the string and add an associative array with the key which coincided from given string

How can I get values from string and put it into an associative array, where the key must be given wildcard string.
Given template is:
param1/prefix-{wildcard1}/{wildcard2}/param2
Given string is:
param1/prefix-name/lastname/param2
The result must be
array('wildcard1' => 'name', 'wildcard2' => 'lastname');
UPD
I want to implement some route script, and wildcards must be variable names that will be injected to script and they will be loaded dynamically from other classes.
I'd first transform template into regex with named capture group, then do the preg_match.
$template = 'param1/prefix-{wildcard1}/{wildcard2}/param2';
// escape special characters
$template = preg_quote($template, '/');
// first, change all {wildcardN} into (?<wildcardN.*?)
$regex = preg_replace('/\\\{([^}]+)\\\}/', "(?<$1>.*?)", $template);
$string = 'param1/prefix-name/lastname/param2';
// do the preg_match using the regex
preg_match("/$regex/", $string, $match);
print_r($match);
Output:
Array
(
[0] => param1/prefix-name/lastname/param2
[wildcard1] => name
[1] => name
[wildcard2] => lastname
[2] => lastname
)

Push an array within an array

I created a function called "keyword_query()" which gets a single string and runs a query with this variable on an API - then the API returns an array (which is defined global so it's changed outside the function too).
The second function "keyword_query_array()" should run the "keyword_query()" more than once, and push to a new array (which is global too) and get an array of several arrays. This function gets a variable of an array of keywords. The function gets the array and navigates through the array without any problem.
Please notice the comments in the code:
<?php
// Runs the query "Research Key" on a keyword and get App ids, names, ect'.
function keyword_query($keyword){
global $research_key_array, $keyword;
// Add the keyword to the "Research Key" query:
$research_key_query = "https://example.com/api/banana/ajax/kaka?term=$keyword&country=US&auth_token=666";
// Create a stream for Json. That's how the code knows what to expect to get.
$context_opts = array(
'http' => array(
'method' => "GET",
'header' => "Accepts: application/json\r\n"
)
);
$context = stream_context_create($context_opts);
// Get the Json
$research_key_json = file_get_contents($research_key_query, false, $context);
// Turn Json to a PHP array
$research_key_array = json_decode($research_key_json, true);
//var_dump($research_key_array);
//print_r($research_key_array);
return $research_key_array;
}
// Runs the keyword_query() function on an array of keywords.
function keyword_query_array($keyword_array){
global $array_of_key_queries;
// Get the last array cell
$last_array_cell = count($keyword_array);
// Navigate through the array
for ($i=0; $i<=$last_array_cell ; $i++) {
//echo $keyword_array[$i]; ****works!
// Error here: Notice: Undefined offset: 3 in C:\wamp\www\PHPExcel\api_fun.php on line 51
array_push( $array_of_key_queries, keyword_query($keyword_array[$i]) );
}
var_dump($array_of_key_queries);
}
But when I get to this line:
array_push( $array_of_key_queries, keyword_query($keyword_array[$i]) ); I get an error of:
Notice: Undefined offset: 3 in C:\wamp\www\PHPExcel\api_fun.php on line 51
with this var_dump:
array (size=4)
0 =>
array (size=1)
'keyword' =>
array (size=0)
empty
1 =>
array (size=1)
'keyword' =>
array (size=0)
empty
2 =>
array (size=1)
'keyword' =>
array (size=0)
empty
3 =>
array (size=1)
'keyword' =>
array (size=0)
empty
What is the right way to push an array within an array like this case?
I changed global $research_key_array, $keyword; from the first function to global $research_key_array;
Works great now!
Thanks #Adelphia !

Array to string conversion error on cakephp cookie

I am trying to set a associative array to a cookie variable in cakephp. The array is :
$recent_designers = array(
"0"=>
array(
"name" => "Hello",
),
"1"=>
array(
"name" => "Hi",
)
);
And to set this array to a cookie recent_designers :
$this->Cookie->write('recent_designers', $recent_designers);
$cookies = $this->Cookie->read('recent_designers');
$this->set("recent_designers", $cookies);
But I am getting an notice
Notice (8): Array to string conversion [CORE\cake\libs\controller\components\cookie.php, line 458] on the ctpfile ! If my array is in this format:
$recent_designers = array(
"0"=>"Hello","1"=>"Hi","2"=>"Namaste"
);
I did not get any error.
you can store a plain array in cakephp using cookie::write, but not a nested array.
http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#using-the-component.
Passing an array of 3 elements is the same of saving 3 cookies
so doing
$recent_designers = array(
"0"=>"Hello",
"1"=>"Hi",
"2"=>"Namaste"
);
$this->Cookie->write('recent_designers', $recent_designers);
is the same of
$this->Cookie->write('recent_designers.0', 'Hello');
$this->Cookie->write('recent_designers.1', 'Hi');
$this->Cookie->write('recent_designers.2', 'Namaste');

Accessing data from CakePHP's find() return array

Here's an example of an array that is returned by CakePHP's find() method:
Array
(
[Tutor] => Array
(
[id] => 2
[PersonaId] => 1
)
)
The official documentation shows how to fetch records, but does not show how to iterate through them or even read out a single value. I'm kind of lost at this point. I'm trying to fetch the [id] value within the array. Here's what I've tried:
// $tutor is the array.
print_r($tutor[0]->id);
Notice (8): Undefined offset: 0
[APP\Controller\PersonasController.php, line 43]
Notice (8): Trying to
get property of non-object [APP\Controller\PersonasController.php,
line 43]
I've also tried:
// $tutor is the array.
print_r($tutor->id);
Notice (8): Trying to get property of non-object [APP\Controller\PersonasController.php, line 44]
The -> way of accessing properties is used in objects. What you have shown us is an array. In that example, accessing the id would require
$tutor['Tutor']['id']
Official PHP documentation, "Accessing array elements with square bracket syntax":
<?php
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]); //"bar"
var_dump($array[42]); //24
var_dump($array["multi"]["dimensional"]["array"]); //"foo"
?>
The returned value is an array, not an object. This should work:
echo $tutor['Tutor']['id'];
Or:
foreach($tutor as $tut){
echo $tut['Tutor']['id'] . '<br />';
}

Categories