Why list() method using two array (array of arrays) as values don't work (for one specific array ($subCategories) the list wrap in another array, the other array is output equals before list()).
list method:
list($subCategories, $subCategoriesName) = [$subCategories, $subCategoriesName]; //[array, array]
[$subCategories, $subCategoriesName] are the return of a function
$subCategories before list
array(4) {
[0]=>
array(3) {
["id"]=>
int(1)
["sub_category_name"]=>
string(14) "Industrilizado"
["category_id"]=>
int(1)
}
[1]=>
array(3) {
["id"]=>
int(2)
["sub_category_name"]=>
string(9) "In natura"
["category_id"]=>
int(1)
}
[2]=>
array(3) {
["id"]=>
int(3)
["sub_category_name"]=>
string(13) "Comida pronta"
["category_id"]=>
int(1)
}
[3]=>
array(3) {
["id"]=>
int(4)
["sub_category_name"]=>
string(11) "Desidratado"
["category_id"]=>
int(1)
}
}
$subCategories after list
array(1) {
[0]=>
array(4) {
[0]=>
array(3) {
["id"]=>
int(1)
["sub_category_name"]=>
string(14) "Industrilizado"
["category_id"]=>
int(1)
}
[1]=>
array(3) {
["id"]=>
int(2)
["sub_category_name"]=>
string(9) "In natura"
["category_id"]=>
int(1)
}
[2]=>
array(3) {
["id"]=>
int(3)
["sub_category_name"]=>
string(13) "Comida pronta"
["category_id"]=>
int(1)
}
[3]=>
array(3) {
["id"]=>
int(4)
["sub_category_name"]=>
string(11) "Desidratado"
["category_id"]=>
int(1)
}
}
Related
I'm grouping one multidimensional array by age.
This is my code:
$mEmployees = array (
array("name"=>"Pedro", "age"=>20, "ID"=>1111),
array("name"=>"Carlos", "age"=>15, "ID"=>2222),
array("name"=>"Susana", "age"=>20, "ID"=>3333),
array("name"=>"Carmen", "age"=>19, "ID"=>4444)
);
$byAge=array();
foreach ($mEmployees as $k => $oneItem) {
$byAge[$oneItem['age']][$k] = $oneItem;
}
var_dump($byAge);
That works fine as you can see below:
output:
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["age"]=>
int(20)
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["age"]=>
int(20)
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["age"]=>
int(15)
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["age"]=>
int(19)
["ID"]=>
int(4444)
}
}
}
But in the results, the age key is redundant. I want to remove this key in the $byAge array.
I tried with array_slice, but it's not possible to indicate one irregular offset (the key age is in middle).
How I can achieve this easily for this result?
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["ID"]=>
int(4444)
}
}
}
Cache the age value in a variable and unset from $oneItem.
foreach ($mEmployees as $k => $oneItem) {
$age = $oneItem['age'];
unset($oneItem['age']);
$byAge[$age][$k] = $oneItem;
}
Demo: https://3v4l.org/pDDn5
Wrote this recursive function to reindex an array (see image below - JSONized Array I work with, to better see it)
Code:
private function reindex($array)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key]=$this->reindex($value);
}
array_values($array);
}
return $array;
}
The goal is to reindex it so there are no jump in indexes i.e
$arr['body'][0]['1']['body'] has elements at indexes 2, 4 and 6, would like to reindex it to be 0, 1, 2
Should work for different arrays also (n amount of nested arrays). Should be general function.
How can I do it?
Thanks,
Update 1:
Var_dump output:
array(4) {
["token_name"]=>
string(6) "C_ROOT"
["token_group"]=>
string(7) "C_BLOCK"
["group"]=>
bool(true)
["body"]=>
array(1) {
[0]=>
array(2) {
[0]=>
array(7) {
["token_name_org"]=>
string(8) "T_SWITCH"
["token"]=>
int(339)
["value"]=>
string(6) "switch"
["line"]=>
int(2)
["token_group"]=>
string(9) "FUNCTIONS"
["token_name"]=>
string(8) "C_SWITCH"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(7) {
["token_name_org"]=>
string(10) "T_VARIABLE"
["token"]=>
int(320)
["value"]=>
string(4) "argv"
["line"]=>
int(2)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(10) "C_VARIABLE"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "1"
["line"]=>
int(2)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
}
}
}
[1]=>
array(4) {
["token_name"]=>
string(7) "C_BLOCK"
["token_group"]=>
string(7) "C_BLOCK"
["group"]=>
bool(true)
["body"]=>
array(4) {
[0]=>
array(1) {
[0]=>
array(4) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(2) "10"
["line"]=>
int(4)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
[2]=>
array(1) {
[0]=>
array(5) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "single_quoted"
["line"]=>
int(7)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
["body"]=>
array(1) {
[0]=>
array(5) {
[0]=>
array(6) {
["token_name_org"]=>
string(10) "T_VARIABLE"
["token"]=>
int(320)
["value"]=>
string(3) "val"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(10) "C_VARIABLE"
}
[1]=>
array(6) {
["token_name_org"]=>
string(18) "C_ASSIGNMENT_EQUAL"
["line"]=>
int(8)
["value"]=>
string(1) "="
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(11) "ASSIGNMENTS"
["token_name"]=>
string(18) "C_ASSIGNMENT_EQUAL"
}
[2]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "3"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
[3]=>
array(6) {
["token_name_org"]=>
string(7) "VALUE_-"
["line"]=>
int(8)
["value"]=>
string(1) "-"
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(9) "OPERATORS"
["token_name"]=>
string(16) "C_OPERATOR_MINUS"
}
[4]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "5"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
}
[4]=>
array(1) {
[0]=>
array(5) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "double_quoted"
["line"]=>
int(11)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
["body"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(7) {
["token_name_org"]=>
string(6) "T_ECHO"
["token"]=>
int(328)
["value"]=>
string(4) "echo"
["line"]=>
int(13)
["token_group"]=>
string(9) "FUNCTIONS"
["token_name"]=>
string(6) "C_ECHO"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "double quoted"
["line"]=>
int(13)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
}
}
}
}
}
}
[6]=>
array(3) {
[0]=>
array(4) {
["token_name_org"]=>
string(9) "T_DEFAULT"
["token"]=>
int(342)
["value"]=>
string(7) "default"
["line"]=>
int(18)
}
[1]=>
array(6) {
["token_name_org"]=>
string(17) "C_SEPARATOR_COLON"
["line"]=>
int(18)
["value"]=>
string(1) ":"
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(10) "SEPARATORS"
["token_name"]=>
string(17) "C_SEPARATOR_COLON"
}
[2]=>
array(6) {
["token_name_org"]=>
string(7) "T_BREAK"
["token"]=>
int(343)
["value"]=>
string(5) "break"
["line"]=>
int(19)
["token_group"]=>
string(16) "TODO_BREAK_GROUP"
["token_name"]=>
string(7) "C_BREAK"
}
}
}
}
}
}
}
Here is a method that will only reindex numeric values:
function reindex($array)
{
$index = 0;
$return = [];
foreach ($array as $key => $value) {
if (is_string($key)) {
$newKey = $key;
} else {
$newKey = $index;
++$index;
}
$return[$newKey] = is_array($value) ? reindex($value) : $value;
}
// Sort alphabetically, numeric first then alpha
ksort($return, SORT_NATURAL);
return $return;
}
Example here: http://ideone.com/969OGa
Ok, So I am working with the reddit api using the php sdk wrapper by #jcleblanc ( https://github.com/jcleblanc/reddit-php-sdk )
That information is not necessarily relevant to the question but I want to give you a frame of reference so you understand what I am looking to do.
The api returns what I believe is a mixed json object as a response when I make calls to reddit. The response length varies and is not consistent. The only this that is consistent is if the action was successful, it returns the key pair
["success"]=> bool(true)
I want to basically search whatever is returned by the api for the key "success" and I can simply evaluate for true or false. Can anybody help with some code to do that?
Here is a sample complete return:
object(stdClass)#1171 (2) { ["jquery"]=> array(29) { [0]=> array(4) { [0]=> int(0) [1]=> int(1) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(4) "body" } } [1]=> array(4) { [0]=> int(1) [1]=> int(2) [2]=> string(4) "attr" [3]=> string(4) "find" } [2]=> array(4) { [0]=> int(2) [1]=> int(3) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(7) ".status" } } [3]=> array(4) { [0]=> int(3) [1]=> int(4) [2]=> string(4) "attr" [3]=> string(4) "hide" } [4]=> array(4) { [0]=> int(4) [1]=> int(5) [2]=> string(4) "call" [3]=> array(0) { } } [5]=> array(4) { [0]=> int(5) [1]=> int(6) [2]=> string(4) "attr" [3]=> string(4) "html" } [6]=> array(4) { [0]=> int(6) [1]=> int(7) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [7]=> array(4) { [0]=> int(7) [1]=> int(8) [2]=> string(4) "attr" [3]=> string(3) "end" } [8]=> array(4) { [0]=> int(8) [1]=> int(9) [2]=> string(4) "call" [3]=> array(0) { } } [9]=> array(4) { [0]=> int(1) [1]=> int(10) [2]=> string(4) "attr" [3]=> string(8) "redirect" } [10]=> array(4) { [0]=> int(10) [1]=> int(11) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(66) "https://www.reddit.com/r/mysubreddit/comments/12hyas2/my_no_link_post/" } } [11]=> array(4) { [0]=> int(1) [1]=> int(12) [2]=> string(4) "attr" [3]=> string(4) "find" } [12]=> array(4) { [0]=> int(12) [1]=> int(13) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(11) "*[name=url]" } } [13]=> array(4) { [0]=> int(13) [1]=> int(14) [2]=> string(4) "attr" [3]=> string(3) "val" } [14]=> array(4) { [0]=> int(14) [1]=> int(15) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [15]=> array(4) { [0]=> int(15) [1]=> int(16) [2]=> string(4) "attr" [3]=> string(3) "end" } [16]=> array(4) { [0]=> int(16) [1]=> int(17) [2]=> string(4) "call" [3]=> array(0) { } } [17]=> array(4) { [0]=> int(1) [1]=> int(18) [2]=> string(4) "attr" [3]=> string(4) "find" } [18]=> array(4) { [0]=> int(18) [1]=> int(19) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(12) "*[name=text]" } } [19]=> array(4) { [0]=> int(19) [1]=> int(20) [2]=> string(4) "attr" [3]=> string(3) "val" } [20]=> array(4) { [0]=> int(20) [1]=> int(21) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(0) "" } } [21]=> array(4) { [0]=> int(21) [1]=> int(22) [2]=> string(4) "attr" [3]=> string(3) "end" } [22]=> array(4) { [0]=> int(22) [1]=> int(23) [2]=> string(4) "call" [3]=> array(0) { } } [23]=> array(4) { [0]=> int(1) [1]=> int(24) [2]=> string(4) "attr" [3]=> string(4) "find" } [24]=> array(4) { [0]=> int(24) [1]=> int(25) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(13) "*[name=title]" } } [25]=> array(4) { [0]=> int(25) [1]=> int(26) [2]=> string(4) "attr" [3]=> string(3) "val" } [26]=> array(4) { [0]=> int(26) [1]=> int(27) [2]=> string(4) "call" [3]=> array(1) { [0]=> string(1) " " } } [27]=> array(4) { [0]=> int(27) [1]=> int(28) [2]=> string(4) "attr" [3]=> string(3) "end" } [28]=> array(4) { [0]=> int(28) [1]=> int(29) [2]=> string(4) "call" [3]=> array(0) { } } } ["success"]=> bool(true) }
The code you gave mean you were calling json_decode($response). This return an object.
It should be:
$resArray = json_decode($response, true)
Which return an associated array.
Then to use:
if (isset($resArray['success'])) {
//your logic here
}
Your object has two properties: jquery and success.
All you need to get the success property is this:
$success = $your_object->success;
As long as the returned object has a success property, that's all you should need to do, regardless of how many other properties it has, or the size of any of its other properties.
If you aren't sure if the object will have the success property, you can use
$success = !empty($your_object->success);
which will evaluate to true if the property exists and is true, and false otherwise.
You might be able to just throw it into
json_decode
If it complains about the format you can do some direct string manipulation beforehand and then you'll be able to reference it directly as an associative array.
Use var_dump to get familiar with the structure after you get a successful parse.
I have a problem to transform my hierarchical array like this:
array(
[0]=>
array(3) {
["id"]=>
int(2353011010)
["name"]=>
string(17) "LEDER ACCESSOIRES"
["order"]=>
int(15)
}
[1]=>
array(3) {
["id"]=>
int(2371475010)
["name"]=>
string(15) "SPORT AUFKLEBER"
["order"]=>
int(25)
}
[2]=>
array(4) {
["id"]=>
int(2563635010)
["name"]=>
string(17) "KENNZEICHENHALTER"
["order"]=>
int(10)
["children"]=>
array(6) {
[0]=>
array(4) {
["id"]=>
int(3854259010)
["name"]=>
string(9) "EDELSTAHL"
["order"]=>
int(92)
["children"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
int(20878056010)
["name"]=>
string(5) "test1"
["order"]=>
int(1)
}
}
}
[1]=>
array(3) {
["id"]=>
int(3854260010)
["name"]=>
string(5) "CHROM"
["order"]=>
int(91)
}
}
[3]=>
array(4) {
["id"]=>
int(19754330010)
["name"]=>
string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
["order"]=>
int(3)
}
}
)
Into a flat ones like this:
array(
[0]=>
array(3) {
["id"]=>
int(2353011010)
["name"]=>
string(17) "LEDER ACCESSOIRES"
["order"]=>
int(15)
}
[1]=>
array(3) {
["id"]=>
int(2371475010)
["name"]=>
string(15) "SPORT AUFKLEBER"
["order"]=>
int(25)
}
[2]=>
array(3) {
["id"]=>
int(2563635010)
["name"]=>
string(17) "KENNZEICHENHALTER"
["order"]=>
int(10)
}
[3]=>
array(4) {
["id"]=>
int(3854259010)
["name"]=>
string(9) "EDELSTAHL"
["order"]=>
int(92),
["parentId"]=> 2563635010
}
[4]=>
array(4) {
["id"]=>
int(20878056010)
["name"]=>
string(5) "test1"
["order"]=>
int(1),
["parentId"]=> 2563635010
}
[5]=>
array(4) {
["id"]=>
int(3854260010)
["name"]=>
string(5) "CHROM"
["order"]=>
int(91),
["parentId"]=> 2563635010
}
[6]=>
array(4) {
["id"]=>
int(19754330010)
["name"]=>
string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
["order"]=>
int(3)
}
)
The children antities should be removed and every child element should get a parentId entity of the higher level id. I need this solution for transfering into DB.
thx
Now, I have create a "temporary" method that works for me, but is noch flexible to use:
function recursive($categories) {
foreach ($categories as $value) {
$result[$value->id]['id'] = $value->id;
$result[$value->id]['name'] = $value->name;
$result[$value->id]['order'] = $value->order;
$result[$value->id]['parentId'] = 0;
if(isset($value->children)) {
$parentId = $value->id;
foreach($value->children as $value2) {
$result[$value2->id]['id'] = $value2->id;
$result[$value2->id]['name'] = $value2->name;
$result[$value2->id]['parentId'] = $parentId;
if(isset($value2->children)) {
$parentId = $value2->id;
foreach($value2->children as $value3) {
$result[$value3->id]['id'] = $value3->id;
$result[$value3->id]['name'] = $value3->name;
$result[$value3->id]['parentId'] = $parentId;
}
}
}
}
}
return $result;
}
Do anybody know a recursive solution for this method?
I have a text .. I went to extract triples from text .. I use Stanford-NLP Library in php Standford-NLP How can I extract triples(subject - object - predicate)?
The example code in the GitHub ReadMe shows how to write the code. The output is a list of word/part-of-speech pairs. Looking at the first example,
"What does the fox say?" becomes:
array(3) {
["wordsAndTags"]=>
array(6) {
[0]=>
array(2) {
[0]=>
string(4) "What"
[1]=>
string(2) "WP"
}
[1]=>
array(2) {
[0]=>
string(4) "does"
[1]=>
string(3) "VBZ"
}
[2]=>
array(2) {
[0]=>
string(3) "the"
[1]=>
string(2) "DT"
}
[3]=>
array(2) {
[0]=>
string(3) "fox"
[1]=>
string(2) "NN"
}
[4]=>
array(2) {
[0]=>
string(3) "say"
[1]=>
string(2) "VB"
}
[5]=>
array(2) {
[0]=>
string(1) "?"
[1]=>
string(1) "."
}
}
["penn"]=>
array(2) {
["parent"]=>
string(4) "ROOT"
["children"]=>
array(1) {
[0]=>
array(2) {
["parent"]=>
string(5) "SBARQ"
["children"]=>
array(3) {
[0]=>
array(2) {
["parent"]=>
string(4) "WHNP"
["children"]=>
array(1) {
[0]=>
array(2) {
["parent"]=>
string(7) "WP What"
["children"]=>
array(0) {
}
}
}
}
[1]=>
array(2) {
["parent"]=>
string(2) "SQ"
["children"]=>
array(3) {
[0]=>
array(2) {
["parent"]=>
string(8) "VBZ does"
["children"]=>
array(0) {
}
}
[1]=>
array(2) {
["parent"]=>
string(2) "NP"
["children"]=>
array(2) {
[0]=>
array(2) {
["parent"]=>
string(6) "DT the"
["children"]=>
array(0) {
}
}
[1]=>
array(2) {
["parent"]=>
string(6) "NN fox"
["children"]=>
array(0) {
}
}
}
}
[2]=>
array(2) {
["parent"]=>
string(2) "VP"
["children"]=>
array(1) {
[0]=>
array(2) {
["parent"]=>
string(6) "VB say"
["children"]=>
array(0) {
}
}
}
}
}
}
[2]=>
array(2) {
["parent"]=>
string(3) ". ?"
["children"]=>
array(0) {
}
}
}
}
}
}
["typedDependencies"]=>
array(5) {
[0]=>
array(3) {
["type"]=>
string(4) "dobj"
[0]=>
array(2) {
["feature"]=>
string(3) "say"
["index"]=>
int(5)
}
[1]=>
array(2) {
["feature"]=>
string(4) "What"
["index"]=>
int(1)
}
}
[1]=>
array(3) {
["type"]=>
string(3) "aux"
[0]=>
array(2) {
["feature"]=>
string(3) "say"
["index"]=>
int(5)
}
[1]=>
array(2) {
["feature"]=>
string(4) "does"
["index"]=>
int(2)
}
}
[2]=>
array(3) {
["type"]=>
string(3) "det"
[0]=>
array(2) {
["feature"]=>
string(3) "fox"
["index"]=>
int(4)
}
[1]=>
array(2) {
["feature"]=>
string(3) "the"
["index"]=>
int(3)
}
}
[3]=>
array(3) {
["type"]=>
string(5) "nsubj"
[0]=>
array(2) {
["feature"]=>
string(3) "say"
["index"]=>
int(5)
}
[1]=>
array(2) {
["feature"]=>
string(3) "fox"
["index"]=>
int(4)
}
}
[4]=>
array(3) {
["type"]=>
string(4) "root"
[0]=>
array(2) {
["feature"]=>
string(4) "ROOT"
["index"]=>
int(0)
}
[1]=>
array(2) {
["feature"]=>
string(3) "say"
["index"]=>
int(5)
}
}
}
}
Then I need to extract triples .. How can I do that?