learner question: array(key,value)

Posted on 2005-03-26
Medium Priority
Last Modified: 2010-08-05
I have flat files whose contents regularly move from one file to another or a different location in the same file or are deleted. The contents of those files could be compared to containers holding other containers, down from 1 to 5 levels and it is those containers that move or are deleted.

Are PHP arrays suitable to trace these containers through their key attribute? For example if one container moves to another file or to another position in the same file (another array or sub-array) can it keep its key automatically?

When moving, can a container's key and value be inserted in a precise location in another array or sub-array?

When saving files from arrays, can the keys be saved in one file and the values in another?

If PHP arrays are suitable, can you show me some example of code for this particular application?
Question by:rblampain
  • 3
  • 2
  • 2
  • +1

Assisted Solution

namitthapa earned 150 total points
ID: 13638731
Well i didn't get what does container mean in your question.But answering your question it is possible to a key and value in another array in a precise location.Like wise we can save file from array also.

Author Comment

ID: 13638896
Another comparison of the files I am talking about is that of a text file divided into words, sentences, paragraphs, chapters and so on, sometimes what is moved is only a word, sometimes it's a sentence or a paragraph etc.
LVL 11

Accepted Solution

ZhaawZ earned 825 total points
ID: 13642953
I'm not sure if I understood what you need.

> When saving files from arrays, can the keys be saved in one file and the values in another?
Do you really need this? You may make a PHP file that will contain an array with keys and values.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 13643370
To ZhaawZ:
I don't have a clear understanding of "keys" yet but what I have to do is to put into arrays all the words, sentences etc (in my last example) and allow them to be moved to other parts of a multi-dimensional array with a reference attached to them showing where they come from. I thought the "keys" might come handy here. So, to answer your question, yes I need it.  It's also needed as a log of the history of the files, this is why I need a separate file with the "keys". Not long ago a versioning system was contemplated but found completely unsuitable.

Assisted Solution

pkphilip earned 525 total points
ID: 13660373
I am not entirely sure I understand your question, but I will try and address this whole issue of array keys. An array is defined something like this:

$first_array = array();
$first_array["line1"] = "This is the contents of line 1"';

Here "line1" is the key and "This is the contents of line 1" is the value.

if you have two arrays and you want to copy the key and value from one array to the second array, you can do something like this:

$second_array = array();
$second_array["line1"] = $first_array["line1"];


Author Comment

ID: 13661117
Thanks to pkphilip. This answers part of my questions.

My second question involves the following arrays example:

$f1 = array("l1" => array("l2" => array("l3" => array("l4" => array("l5" => array())))));
$f2 = array("l1" => array("l2" => array("l3" => array("l4" => array("l5" => array())))));

as I understand it, this is the way to define a multi-dimensional array, I've called the arrays $f1 and $f2 and I've made what I think is 5 levels of dimensions, these dimensions are the containers I mentioned earlier. What I want to do is, for example, move someting from $f1
level 3 to $f2 level 3,  this would automatically imply moving everyting below in level 4 and level 5.
Is this doable? Is this a normal use of arrays? Are there functions that do this or would it need to be done as a script?
Again, a few examples are most welcome.
LVL 11

Expert Comment

ID: 13664704
Array is one of types. "Multidimensional arrays" actually are just arrays of arrays. For example:

$arr1[0] = 'one';
$arr1[1] = 'two';
$arr1[2] = 'three';

$arr2[0] = $arr1;
$arr2[1] = $arr1;  // this will make array(array('one', 'two', 'three'), array('one', 'two', 'three'))
unset($arr2[1]);   // this will delete $arr2[1].. $arr2 is now array(array('one', 'two', 'three'))

$arr3 = $arr1; // this will "copy" $arr1
$arr4 = $arr2; // this will "copy" $arr2 (with all "subarrays")

You may use array variables the same way you use other variables.

Expert Comment

ID: 13679386
I wondered Why Did u accept my comment as an answer.Actually these time i was quite busy so i didn't have time to return here.Any Way Thank you.

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question